不同场景转换时的奇怪行为

时间:2013-12-29 17:07:13

标签: ios objective-c cocos2d-iphone ccscene

我希望在游戏发布之前制作一些自定义图片(来自游戏开发者的一些徽标和信息)。在cocos2d寺庙中,只有一个IntroLayer来显示徽标,所以我决定添加更多场景来显示其他进入游戏主菜单之前的信息。在IntroLayer.h中

//this is the template code    
@interface IntroLayer : CCLayer
{
}

+(CCScene *) scene;

@end

//this is the new scene,I hope to display after the IntroLayer
@interface SecondScene : CCLayer
{
}

+(CCScene*) scene;

@end

//In IntroLayer.m
@implementation IntroLayer

//I change the replaceScene function from [HelloWorldLayer node] to[SecondScene scene]
-(void) onEnter
{
    [super onEnter];
    [[CCDirector sharedDirector] replaceScene:
     [CCTransitionFade transitionWithDuration:1.0 scene:[SecondScene scene]]];
}
@end

@implementation SecondScene

+(CCScene*)scene
{
    CCScene *scene = [CCScene node];
    SecondScene *layer = [SecondScene node];
    [scene addChild:layer];
    return scene;
}

-(id)init
{
    if (self = [super init]) {
        CGSize size = [[CCDirector sharedDirector] winSize];
        CCSprite *background = [CCSprite spriteWithFile:@"LOGO.png"];
        background.position = ccp(size.width/2, size.height/2);
        [self addChild: background];
    }
    return  self;
}

//I hope the main game scene display after the SecondScene
-(void)onEnter{
    [[CCDirector sharedDirector] replaceScene:
     [CCTransitionFade transitionWithDuration:1.0 scene:[HelloWorldLayer scene]]];
    [super onEnter];
}

@end

但是当我运行这段代码时,我发现它没有按照我的预期运行.SecondScene和HelloWorldLayer几乎同时显示,实际上最后只有SecondScene在屏幕上留下,HelloWorldLayer很快就消失了。我真的很困惑。任何人都可以给出一些建议吗?真的很感谢你。

1 个答案:

答案 0 :(得分:0)

如果您希望第二次转换遵循第一次转换,则需要覆盖onEnterTransitionDidFinish方法:

-(void)onEnterTransitionDidFinish{
    [super onEnterTransitionDidFinish];
    [[CCDirector sharedDirector] replaceScene:
     [CCTransitionFade transitionWithDuration:1.0 scene:[HelloWorldLayer scene]]];
}