How to display a continously chaning sprite in the background?


I am using a Sprite to display the background. I am changing the sprite’s image dynamically at regular intervals. But, the game character sprites are being hidden in the background. How can make the character sprites always overlap the background sprite.

I have tried changing the z-order in
void addChild ( CCNode * child, int zOrder, int tag )
but it didn’t work.

I also tried setting the sprite->setVertexZ(0) with no success.
Which is the best way to do this?


You could put the background in its own CCLayer.

CCLayer* bgLayer = CCLayer::create();

CCLayer* gameLayer = CCLayer::create();
//... add all other sprites to gameLayer


// maybe you want another layer on top for UI stuff
CCLayer* UILayer = CCLayer::create();



Thanks Adam, I’ll try that.
Btw, I was able to get my code to work by using a negative value for zOrder. I know it was really silly of me, but I couldn’t find any documentation for zOrder and was assuming its range is only positive.


Children with a z-order less then zero are drawn before the parent sprite.
Cocos2dx will draw a node like this:

  • draw all children with zOrder < 0
  • draw the actual node
  • draw all children with zOrder >= 0


Andre, exactly what I needed to get started. We should definitely add thiszOrder@ doc to the wiki for people like me who have jumped on to cocos2dx without having any prior experience with cocos2d.