Ale*_*lex 2 z-index objective-c cocos2d-iphone
我正在玩cocos2d并想知道我怎么能给精灵一个层深度意味着我怎样才能将精灵放在别人之上呢?
你可以这样做:
假设您的类是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;
}
Run Code Online (Sandbox Code Playgroud)
你需要学习如何使用的是add:add的z:参数.如果添加不带z参数的子项,则将子项置于顶部.