odd behavior when transition of different scenes

91 Views Asked by At

I hope to make some custom picture(e.x some logo and information of the game developer)before game is launched.In the cocos2d temple,there is only a IntroLayer to show logo,so I decide to add more scene to display other information before entering the main menu of the game.In 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

But when I run this code,I found it doesn't run as I expected.The SecondScene and the HelloWorldLayer display nearly at the same time,actually at last there is only SecondScene left in the screen and the HelloWorldLayer quickly disappear.I am really confused with that.Can anyone give some advice?Really Thanks for that.

1

There are 1 best solutions below

1
On

If you want the second transition to follow the first, you need to override the onEnterTransitionDidFinish method:

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