質問

I am creating a fighting game. I've got a spritesheet for the hero and a spritesheet for a monster. Since both will be able to do multiple attacks I would like to avoid to merge them in a single spritesheet. And I am intending to create more characters.

Now I am creating a SpriteBatchNode for each spritesheet and add them to layer.

CCLayer* stage = CCLayer::create();
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("character_hero.plist");
this->characterHeroBatchNode = CCSpriteBatchNode::create("character_hero.pvr.ccz");

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("character_monster.plist");
this->characterMonsterBatchNode = CCSpriteBatchNode::create("character_monster.pvr.ccz");

stage->addChild(characterHeroBatchNode);
stage->addChild(characterMonsterBatchNode);

Now I have one sprite in characterHeroBatchNode and multiple in characterMonsterBatchNode.

How can I reorder the monsters and the heros z-Order based on their PositionY attribute.

For example>

monster1->setPositionY(10); // In monster batch node
hero->setPositionY(24); // In hero batch node
monster2->setPositionY(43); // In monster batch node

I want the monster 1 behind the hero. And the hero behind monster2.

役に立ちましたか?

解決

In the past, I've had to create games where I wanted some sprites in the foreground and others in the background. The sprites were in different sheets, like you have them.

To get them in the order I wanted, I put the "front" sprites into one CCLayer and the "back" sprites into another CCLayer. I added the layers to the scene in the order I wanted them to appear. I also manipulated each the respective layers to make them fade in/out as needed. So you could have the hero in the scene and then have the enemies appear behind him.

If you want to mix and match, you can have 3 layers, one for the "middle ground", one for "front", and one for "back", and dynamically move the sprites between the layers.

Was this what you were looking for?

他のヒント

If I understand what you're asking, the answer is, you can't. Consider each CCSpriteBatchNode to be its own container. You can adjust the Z order of the sprites inside the CCSpriteBatchNode, but when you add the batch to the CCLayer the whole CCSpriteBatchNode is applied to the CCLayer in whatever Z order you added it at. So in order to do what you want you would need another batch of monsters. Or use CCSprites and add/adjust them on the CCLayer in the way you want, but then you obviously lose the CCSpriteBatchNode benefits.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top