在学习马里奥时,我们学习到从菜单场景到游戏场景的切换,代码如下
void CMMenuScene::OnStartCallBack( CCObject *pSender ) { CCDirector* pDirector = CCDirector::sharedDirector(); CCScene *pScene = CMGameScene::CreateGameScene(); pDirector->replaceScene(pScene); }
在cocos2dx里,CCDirector和CCScene有很密切的关系,CCDirector是导演,负责CCScene的展示,切换,结束等工作,具体体现在成员函数中:
/** Enters the Director's main loop with the given Scene. * Call it to run only your FIRST scene. * Don't call it if there is already a running scene. * * It will call pushScene: and then it will call startAnimation */ void runWithScene(CCScene *pScene); /** Suspends the execution of the running scene, pushing it on the stack of suspended scenes. * The new scene will be executed. * Try to avoid big stacks of pushed scenes to reduce memory allocation. * ONLY call it if there is a running scene. */ void pushScene(CCScene *pScene); /** Pops out a scene from the queue. * This scene will replace the running one. * The running scene will be deleted. If there are no more scenes in the stack the execution is terminated. * ONLY call it if there is a running scene. */ void popScene(void); /** Pops out all scenes from the queue until the root scene in the queue. * This scene will replace the running one. * Internally it will call `popToSceneStackLevel(1)` */ void popToRootScene(void); /** Pops out all scenes from the queue until it reaches `level`. If level is 0, it will end the director. If level is 1, it will pop all scenes until it reaches to root scene. If level is <= than the current stack level, it won't do anything. */ void popToSceneStackLevel(int level); /** Replaces the running scene with a new one. The running scene is terminated. * ONLY call it if there is a running scene. */ void replaceScene(CCScene *pScene);
void runWithScene(CCScene *pScene):该函数是刚开始运行时,导演类展示场景
void pushScene(CCScene* scene):该函数将新的scene压入栈。在程序中有一个栈,可以保存很多场景,这样方便后退之类的操作。
void popScene(void):弹出,与pushScene对应
void popToRootScene(void):弹出栈中除了第一个场景外的其他场景
void popToSceneStackLevel(int level):弹出到制定位置的scene
void replaceScene(CCScene *pScene):让新的scene代替栈中当前的场景
有的时候,简单的场景切换显得单调,如果需要动画切换,则需要以下类之一:
HelloWorld2* scene = HelloWorld2::create(); //CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2, (CCScene*)scene)); //CCDirector::sharedDirector()->replaceScene(CCTransitionFadeBL::create(1.2, (CCScene*)scene)); //CCDirector::sharedDirector()->replaceScene(CCTransitionFadeTR::create(1.2, (CCScene*)scene)); //CCDirector::sharedDirector()->replaceScene(CCTransitionTurnOffTiles::create(1.2, (CCScene*)scene)); //CCDirector::sharedDirector()->replaceScene(CCTransitionTurnOffTiles::create(1.2, (CCScene*)scene)); //CCDirector::sharedDirector()->replaceScene(CCTransitionJumpZoom::create(1.2, (CCScene*)scene)); //CCDirector::sharedDirector()->replaceScene(CCTransitionMoveInL::create(1.2, (CCScene*)scene)); //CCDirector::sharedDirector()->replaceScene(CCTransitionPageTurn::create(1.2, (CCScene*)scene, false)); //CCDirector::sharedDirector()->replaceScene(CCTransitionRotoZoom::create(1.2, (CCScene*)scene)); //CCDirector::sharedDirector()->replaceScene(CCTransitionShrinkGrow::create(1.2, (CCScene*)scene)); //CCDirector::sharedDirector()->replaceScene(CCTransitionSlideInL::create(1.2, (CCScene*)scene)); CCDirector::sharedDirector()->replaceScene(CCTransitionCrossFade::create(1.2, (CCScene*)scene));
使用切换场景来进行场景切换,切换场景类如下所列:
CCTransitionRotoZoom//从大到小画面缩小并跳动进入
CCTransitionJumpZoom//立体从左边缩小跳动进入
CCTransitionMoveInL//从左边移动进入右边
CCTransitionMoveInR//从右边移动进入左边
CCTransitionMoveInT//从上边移动到入下边
CCTransitionMoveInB//从下边移动到入上边
CCTransitionSlideInL//从左边移动入右边
CCTransitionSlideInR//从右边移动入左边
CCTransitionSlideInT//从上边移动入下边
CCTransitionSlideInB//从下边移动入上边
CCTransitionShrinkGrow//从大到小在中间缩小进入
CCTransitionFlipX//从X轴方向立体翻转
CCTransitionFlipY//从Y轴方向立体翻转
CCTransitionFlipAngular//从右边头翻转进入
CCTransitionZoomFlipX//从X轴方向立体跳动翻转
CCTransitionZoomFlipY//从Y轴方向立体跳动翻转
CCTransitionZoomFlipAngular//从右边立体缩小翻转进入
CCTransitionFade//从中间渐变进入
CCTransitionCrossFade//从外围渐变进入
CCTransitionTurnOffTiles//从格子覆盖上层进入
CCTransitionSplitCols//竖直分三个方块切入
CCTransitionSplitRows//横向分三个方块切入
CCTransitionFadeTR//从左下角向右上角格子渐变进入
CCTransitionFadeBL//从右上角角向左下角格子渐变进入
CCTransitionFadeUp//从下向上渐变进入
CCTransitionFadeDown//从上向下渐变进入