开发者

Draw sprite in cocos2d on top of others

I am playing in cocos2d and wanted know how can i give a sprite a layer depth meaning开发者_Python百科 how can i keep a sprite on top of others?


You could do something like this:

Assuming your class is a subclass of CCScene

-(id) init
{
    if( (self=[super init] )) {
        CCLayer *foreground = [CCLayer node];
        CCLayer *background = [CCLayer node];

        CCSprite *sprite1 = [CCSprite spriteWithFile:@"sprite1.png"];
        CCSprite *sprite2 = [CCSprite spriteWithFile:@"sprite2.png"];
        CCSprite *sprite3 = [CCSprite spriteWithFile:@"sprite3.png"];

        [sprite1 addChild:sprite2 z:-1];   //This z:-1 means that sprite 2 is behind sprite 1

        [foreground addChild:sprite1];
        [background addChild:sprite3];

        [self addChild:background z:0];   // z:0 is default, you don't need to add it.
        [self addChild:foreground z:1];   // z:1 is infront of z:0

    }
    return self;

}

The bit you need to learn how to use is the z: parameter of add child. If you add a child without the z parameter, the child is placed on top.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜