cocos2dx之背景Fade in/out

在cocos2dx中,如果我们直接切换背景的话,我们直接使用:

CCDirector::sharedDirector()->replaceScene(pScene);

但是有时候,我们希望在切换背景的时候尽情fade in/out,这个时候我们需要用CCTransitionFade这个类,它继承了CCTransitionScene,我们来看一下如何切换背景时,进行fade in/out:


    CCScene* pScene = CCScene::create();
    ///

    CCLayer* pLayer = new TestController(); 
    pLayer->autorelease();

    pScene->addChild(pLayer); // 注意,这里的一定要是创建出来的pScene否则的话,会添加到this->addChild,在删掉的时候,该this会被删掉,导致你的next layer出不来

    CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(2, pScene, ccWHITE) ); //调用了CCTransitionFade,去create一个fade in/out 对象,返回ne//w的scene


如下式两个scene之间进行fade in/out

void HelloWorld::menuCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCScene *scene = CCScene::create();
    CCLayer *pMyLayer = g_CreateAnMyHelloLayer();
    pMyLayer->autorelease();
    scene->addChild(pMyLayer);
   CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(2, scene, ccWHITE) );

}

void MyHelloWorld::CallMainMenu(cocos2d::CCObject *pObject) {
    CCScene *scene =CCScene::create();
    CCLayer *layer = new HelloWorld;
    layer->init();
    layer->autorelease();
   scene->addChild(layer);
   CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(2, scene, ccWHITE) );
}


你可能感兴趣的:(cocos2dx之背景Fade in/out)