views:

115

answers:

2

I'm using CCSprite to redraw my app's background image (it's like a moving background). However I'm also using CCSprite to draw my hero and enemies. At some cases the enemies goes behind my background image. My understanding is that if you do:

[self addChild:sprite1];
[self addChild:sprite2];

Then sprite1 will be behind sprite2 if they intersect each other in the window. However is there any way to bring sprite1 back to front in case they intersect sprite2? I tried looking at the documentation but cocos2d doesn't seem to have the method that I'm looking...

A: 

I have not used cocos2d... In general If you want to bring sprite1 on the top of sprite2

[self replaceChild:sprite2 withSprite: sprite1]

similarly If you want to bring sprite2 on the top of sprite1

[self replaceChild:sprite1 withSprite: sprite2]

You need to implement replaceChild method... Usually addChild method adds element to an array, You can use replaceObjectAtIndex method if it is array.

Chandan Shetty SP
Hmm that could be working, I can imagine the replace child method will involve deleting the background sprite and creating it again. I'm still wondering if there's any direct method to this in the cocos2d library. It seems that this method is a standard method in sprite objects.
the_great_monkey
A: 

It's been a while since I visited this but sounds like you want to maintain a z-order for your sprites. Cocos2D does actually have support for this in the addChild call. I don't entirely recall if this will solve things for you but it's a good starting point.

Rob Segal