Scene(场景)

[size=small;]原地址:[/size] http://hi.baidu.com/you5a_com/item/1c57d52dba2cc90672863e4d




场景初始化:
+(id) scene {

  CCScene *scene = [CCScene node];

  CCLayer* layer = [HelloWorld node];

  [scene addChild:layer];

  return scene;

}


第一个创建场景的地方是在AppDelegate中aplicationDidFinishLaunching方法结束处.

[[CCDirector sharedDirector] runWithScene:[HelloWorld scene]];

用replaceScene方法来替换已有的场景:
[[CCDirector sharedDirector] replaceScene:[HelloWorld scene]];


场景上下左右场景切入方式:
//从右往左切入
-(void)TurnToNextScene:(id)sender{

[[SimpleAudioEngine sharedEngine]stopBackgroundMusic];

[[CCDirector sharedDirector]replaceScene:[CCTransitionSlideInR transitionWithDuration:1.0 scene:[PaperWindWill_iPhone scene]]];
}

//从左往右切入
-(void)TurnToPresviousScene:(id)sender{

[[SimpleAudioEngine sharedEngine]stopBackgroundMusic];

[[CCDirector sharedDirector]replaceScene:[CCTransitionSlideInL transitionWithDuration:1.0 scene:[LittleCarpenter_iPhone scene]]];

}

//从下往上切入

-(void)TurnToMenuScene:(id)sender{

[[SimpleAudioEngine sharedEngine]stopBackgroundMusic];

[[CCDirector sharedDirector]replaceScene:[CCTransitionSlideInT transitionWithDuration:1.0 scene:[Menu_iPhone scene]]];

}

//从上往下切入

[[CCDirector sharedDirector]replaceScene:[CCTransitionSlideInB transitionWithDuration:1.0scene:[Menu_iPhone scene]];


暂停、返回

[[CCDirector sharedDirector] pause];

[[CCDirector sharedDirector] resume];


推进(Pushing)和弹出(Popping)场景:

设置场景:

[[CCDirector sharedDirector] pushScene:[Settings scene]];

关闭场景:

[[CCDirector sharedDirector] popScene];


用CCDirector replaceScene方法替换场景时, 每个节点都会调用CCNode所带的三个方法.

这三个方法是: onEnter, onEnterTransitionDidFinish和onExit
-(void) onEnter {
    // 节点调用init方法以后将会调用此方法
    // 如果使用了CCTransitionScene,将会在过渡效果开始以后调用此方法

    [super onEnter];
}
-(void) onEnterTransitionDidFinish {
    // 调用onEnter以后将会调用此方法
    // 如果使用了CCTransitionScene,将会在过渡效果结束以后调用此方法
    [super onEnterTransitionDidFinish];
}
-(void) onExit {
    // 节点调用dealloc方法之前将会调用此方法
    // 如果使用了CCTransitionScene,将会在过渡效果结束以后调用此方法

    [super onExit];
}


警告:永远不要在一个节点的init方法中调用CCDirector的replaceScene方法

你可能感兴趣的:(onEnter,replaceScene,CCDirector,CCScene)