开发者

how to make a sprite goes behind another sprite

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 enemie开发者_高级运维s. 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...


You could also pass z as an argument to addChild.

[self addChild:foo z:0]
[self addChild:baz z:1]


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜