欢迎转载:http://blog.csdn.net/fylz1125/article/details/8533970
吐槽一下,刚写了一个小时,这CSDN不知抽什么风就是发不了,我以为存草稿了就刷新了一下,结果没存草稿。擦...
cocos2d-x引擎实现的场景切换特效有41个,我这里就不列举了。有兴趣的童鞋自行看源码或谷歌之,一大把介绍的。昨天看了红孩儿的场景切换,写的很详细,代码逐行注释。我这里不想详细解释代码,只侧重流程。
cocos2d-x里面的场景,分为两类
1.普通场景,就是CCScene::create()出来的。这类没有特效,运行的话直接就出来了。
2.过渡场景,就是有特效的场景,我称之为过渡场景。这类场景切换的时候有特效。
刚写了一遍,实在懒的详细写了。直接说结论:
所谓的过渡场景,就是将你create出来的场景包装了一下,比如
CCScene *scene = About::scene(); CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2, scene));
就CCTransitionFade为例子看下:
class CC_DLL CCTransitionFade : public CCTransitionScene { protected: ccColor4B m_tColor; public: CCTransitionFade(); virtual ~CCTransitionFade(); /** creates the transition with a duration and with an RGB color * Example: FadeTransition::create(2, scene, ccc3(255,0,0); // red color */ static CCTransitionFade* create(float duration,CCScene* scene, const ccColor3B& color); static CCTransitionFade* create(float duration,CCScene* scene); /** initializes the transition with a duration and with an RGB color */ virtual bool initWithDuration(float t, CCScene*scene ,const ccColor3B& color); virtual bool initWithDuration(float t,CCScene* scene); virtual void onEnter();// 过渡回调 virtual void onExit(); };
看这里,它实现了onEnter。在这个函数里面才是实质上的过渡特效实现,就是一系列的动作。动作完了调finish(),finish里面又调setNewScene函数,这个函数比较重要:
void CCTransitionScene::setNewScene(float dt) { CC_UNUSED_PARAM(dt); // [self unschedule:_cmd]; // "_cmd" is a local variable automatically defined in a method // that contains the selector for the method this->unschedule(schedule_selector(CCTransitionScene::setNewScene)); CCDirector *director = CCDirector::sharedDirector(); // Before replacing, save the "send cleanup to scene" m_bIsSendCleanupToScene = director->isSendCleanupToScene(); director->replaceScene(m_pInScene); // 看这里,这是后面要用的 // enable events while transitions director->getTouchDispatcher()->setDispatchEvents(true); // issue #267 m_pOutScene->setVisible(true); }
看主循环如何调度:直接看setNextScene函数
void CCDirector::setNextScene(void) { // 布尔变量,当前场景是过渡场景则true,反之false bool runningIsTransition = dynamic_cast<CCTransitionScene*>(m_pRunningScene) != NULL; // 布尔变量,载入的场景是过渡场景则true,反之false bool newIsTransition = dynamic_cast<CCTransitionScene*>(m_pNextScene) != NULL; // If it is not a transition, call onExit/cleanup // 如果载入的不是一个过渡场景,那么调用当前场景的onExit和cleanup if (! newIsTransition) { if (m_pRunningScene)// 有可能是个过渡场景 { m_pRunningScene->onExitTransitionDidStart(); m_pRunningScene->onExit(); } // issue #709. the root node (scene) should receive the cleanup message too // otherwise it might be leaked. if (m_bSendCleanupToScene && m_pRunningScene) { m_pRunningScene->cleanup(); } } if (m_pRunningScene) { m_pRunningScene->release(); } // 当前场景指针指向载入场景 m_pRunningScene = m_pNextScene; m_pNextScene->retain(); m_pNextScene = NULL; // 如果当前场景不是一个过渡场景,那么调用载入场景的onEnter和onEnterTransitionDidFinish if ((! runningIsTransition) && m_pRunningScene) { m_pRunningScene->onEnter();// 本质是调onEnter m_pRunningScene->onEnterTransitionDidFinish(); } }
一开始两个布尔变量并不总是true,即使你每个场景都用了特效。原因如下:
看前面特效场景类的setNewScene函数,有一句director->replaceScene(m_pInScene)。
这个就将你的场景(CCScene::create()出来的那个,不是你包装的特效场景)设为要载入场景,而这个场景不是一个过渡场景,呵呵。
如果你调试TestCpp的TransitionsTest列子,你会发现这两个布尔变量总是一个true,一个false,呵呵。
本质上来说,一个过渡场景就是实现了CCScene的onEnter系列函数。可以仿照已有的列子自己写,嘿嘿。
就写这么多了,不知道说清楚了没有。