Note that there are some explanatory texts on larger screens.

plurals
  1. POCocos2d: CCSpriteBatchNode grandchildren z-order displaying in wrong order
    text
    copied!<p>I thought I understood how z order works in cocos2d, but I'm having an issue I can't explain. </p> <p>I have a number of CCSprites as children of a CCSpriteBatchNode, created with the code below. Each of these sprites has several children CCSprites. Everything works fine as long as the z values I pass to addChild are positive. When they are negative, it stops working as I expect, and I can't find any information about why this could be the case. I am using cocos2d version 1.0.1.</p> <pre><code> CCSprite *pikaSprite = [CCSprite spriteWithSpriteFrameName:@"PikaLarge.png"]; [pikaSprite setAnchorPoint:ccp(0.0f, 0.5f)]; [pikaSprite setPosition:ccp(pikaX, pikaBox.boundingBox.size.height * 0.6f)]; [pikaBatchNode addChild:pikaSprite z:0]; CCSprite *sprite1 = [CCSprite spriteWithSpriteFrameName:@"Sprite1.png"]; [sprite1 setPosition:ccp(pikaSprite.boundingBox.size.width * 0.7f, pikaSprite.boundingBox.size.height * 0.9f)]; [pikaSprite addChild:sprite1 z:-1]; CCSprite *sprite2 = [CCSprite spriteWithSpriteFrameName:@"Sprite2.png"]; [sprite2 setPosition:ccp(pikaSprite.boundingBox.size.width * 0.8f, pikaSprite.boundingBox.size.height * 0.9f)]; [pikaSprite addChild:sprite2 z:-10]; </code></pre> <p>sprite2 appears in front of sprite1, but behind pikaSprite. As I understand it, it should be behind sprite1, because they are siblings and sprite2 has a lower z value.</p> <p>I want sprite2 to be behind sprite1, both behind pikaSprite, which I can't get unless I change the order I add them, which is possible, but somewhat more difficult.</p> <p>I've tried four different orderings, this is the only one that doesn't work.</p> <p>1.</p> <pre><code> [pikaSprite addChild:sprite1 z:1]; ... [pikaSprite addChild:sprite2 z:10]; // sprite2 is in front, correct </code></pre> <p>2.</p> <pre><code> [pikaSprite addChild:sprite1 z:10]; // sprite1 is in front, correct ... [pikaSprite addChild:sprite2 z:1]; </code></pre> <p>3.</p> <pre><code> [pikaSprite addChild:sprite1 z:-10]; ... [pikaSprite addChild:sprite2 z:-1]; // sprite2 is in front, correct </code></pre> <p>4.</p> <pre><code> [pikaSprite addChild:sprite1 z:-1]; ... [pikaSprite addChild:sprite2 z:-10]; // sprite2 is in front - ??? </code></pre> <p>When they are negative, it seems the last one added is always in front.</p> <p>It would be possible to rewrite things to deal with this, but I'm very curious why this is happening and wondering if I might be misunderstanding something.</p> <p>Thanks for any insight you can give me.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload