mm24
mm24

Reputation: 9586

Cocos2d: Setting zOrder at run time for CCSprite batched in sprite nodes doesn't seem to work

I was able to call those methods at run time on a CCSprite object, now that I decided to batch it in a CCSpriteBatchNode it doesn't seem to work anymore:

sprt = [CCSprite spriteWithSpriteFrameName:@"sprt.png"];
sprt.anchorPoint = CGPointMake(0.5f, 0.5f);
sprt.position = CGPointMake(40.0f, 60.0f);
[batchNode addChild:sprt z:-1];  // It used to work when I was simply "adding as child" the sprt object, I guess now doesn't set the order anymore because somehow the CCSpriteBatch node doesn't allow the re-ordering of child added to it

CCCallFunc *callback = [CCCallFunc actionWithTarget:self selector:@selector(moveBackwards)];
CCCallFunc *callback2 = [CCCallFunc actionWithTarget:self selector:@selector(moveForward)];

[sprt runAction: [CCRepeatForever actionWithAction: [CCSequence actions:  callback2,  callback ,  nil]]];

-(void) moveBackwards
{
    [sprt setZOrder:-1];
 }

-(void) moveForward
{
    [sprt setZOrder:1];
}

Upvotes: 1

Views: 1688

Answers (2)

John D.
John D.

Reputation: 558

Just in case someone else runs across this issue, the following will work to reorder the sprite...

[self.parent reorderChild:self z:x];

Upvotes: 2

CodeSmile
CodeSmile

Reputation: 64477

If you have a CCSpriteBatchNode, you have to consider all the CCSprite to be on the same layer (the spritebatch node).

This means that sprites in a sprite batch node can change their z order relative to other sprites in the sprite batch node, but you can not change the z order to make a sprite-batched sprite appear behind or in front of another node that is not a child of the sprite's CCSpriteBatchNode.

I'm pretty sure this is the problem you've run into. If you have trouble grasping this, consider the CCSpriteBatchNode has the same behavior regarding z ordering as a CCLayer (respectively any other node but devs seem to be hung up on CCLayer as the only/main layering construct). Maybe that makes it easier to understand.

Upvotes: 2

Related Questions