// // HelloWorldScene.h // #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "cocos-ext.h" #include "TestLayer.h" USING_NS_CC; USING_NS_CC_EXT; class HelloWorld : public cocos2d::CCLayer { public: virtual bool init(); static cocos2d::CCScene* scene(); CREATE_FUNC(HelloWorld); void changeScene(); // CCNode生命周期函数 virtual void onEnter(); virtual void onEnterTransitionDidFinish(); virtual void onExit(); // 解释上面3个生命周期函数 /* virtual void onEnter(); 当init()函数执行完成后,默认进入onEnter()生命周期函数 virtual void onEnterTransitionDidFinish(); 当A场景切换到B场景时,过度动画执行完成后默认调用B场景的onEnterTransitionDidFinish()生命周期函数 virtual void onExit(); 当场景退出后,默认调用此生命周期函数 */ // A场景和B场景切换时的生命周期函数调用顺序如下: /* 1、B场景的init函数 2、B场景的OnEnter函数 3、A场景的OnExit函数 4、B场景的onEnterTransitionDidFinish函数 5、A场景的析构函数 */ }; #endif
// // HelloWorldScene.cpp // #include "HelloWorldScene.h" USING_NS_CC; CCScene* HelloWorld::scene() { CCScene *scene = CCScene::create(); HelloWorld *layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSprite *pSpr = CCSprite::create("background1.png"); pSpr->setPosition(ccp(240,160)); addChild(pSpr); CCLabelTTF *labelTTF =CCLabelTTF::create("点击切换场景","Helvetica-Hald",30); labelTTF->setColor(ccRED); CCMenuItemLabel *labelItem = CCMenuItemLabel::create(labelTTF,this,menu_selector(HelloWorld::changeScene)); CCMenu *menu = CCMenu::create(labelItem,NULL); addChild(menu); return true; } void HelloWorld::changeScene() { CCTransitionScene *reScene = NULL; CCScene *s = TestLayer::scene(); float t = 1.2f; // CCTransitionJumpZoom reScene = CCTransitionJumpZoom::create(t,s); // CCTransitionProgressRadialCCW reScene = CCTransitionProgressRadialCCW::create(t,s); // CCTransitionProgressRadialCW /*reScene = CCTransitionProgressRadialCW::create(t,s); // CCTransitionProgressHorizontal reScene = CCTransitionProgressHorizontal::create(t,s); // CCTransitionProgressVertical reScene = CCTransitionProgressVertical::create(t,s); // CCTransitionProgressInOut reScene = CCTransitionProgressInOut::create(t,s); // CCTransitionProgressOutIn reScene = CCTransitionProgressOutIn::create(t,s); // CCTransitionCrossFade reScene = CCTransitionCrossFade::create(t,s); // CCTransitionPageTurn reScene = CCTransitionPageTurn::create(t,s,false); // CCTransitionFadeTR reScene = CCTransitionFadeTR::create(t,s); // CCTransitionFadeBL reScene = CCTransitionFadeBL::create(t,s); // CCTransitionFadeUp reScene = CCTransitionFadeUp::create(t,s); // CCTransitionFadeDown reScene = CCTransitionFadeDown::create(t,s); // CCTransitionTurnOffTiles reScene = CCTransitionTurnOffTiles::create(t,s); // CCTransitionSplitRows reScene = CCTransitionSplitRows::create(t,s); // CCTransitionSplitCols reScene = CCTransitionSplitCols::create(t,s); // CCTransitionFade reScene = CCTransitionFade::create(t,ccRED); // CCTransitionFlipX reScene = CCTransitionFlipX::create(t,kCCTransitionOrientationLeftOver); // CCTransitionFlipY reScene = CCTransitionFlipY::create(t,kCCTransitionOrientationLeftOver); // CCTransitionFlipAngular reScene = CCTransitionFlipAngular::create(t,kCCTransitionOrientationLeftOver); // CCTransitionZoomFlipX reScene = CCTransitionZoomFlipX::create(t,kCCTransitionOrientationLeftOver); // CCTransitionZoomFlipY reScene = CCTransitionZoomFlipY::create(t,kCCTransitionOrientationLeftOver); // CCTransitionZoomFlipAngular reScene = CCTransitionZoomFlipAngular::create(t,kCCTransitionOrientationLeftOver); // CCTransitionShrinkGrow reScene = CCTransitionShrinkGrow::create(t,s); // CCTransitionRotoZoom reScene = CCTransitionRotoZoom::create(t,s); // CCTransitionMoveInL reScene = CCTransitionMoveInL::create(t,s); // CCTransitionMoveInR reScene = CCTransitionMoveInR::create(t,s); // CCTransitionMoveInT reScene = CCTransitionMoveInT::create(t,s); // CCTransitionMoveInB reScene = CCTransitionMoveInB::create(t,s); // CCTransitionSlideInL reScene = CCTransitionSlideInL::create(t,s); // CCTransitionSlideInR reScene = CCTransitionSlideInR::create(t,s); // CCTransitionSlideInT reScene = CCTransitionSlideInT::create(t,s); // CCTransitionSlideInB reScene = CCTransitionSlideInB::create(t,s);*/ CCDirector::sharedDirector()->replaceScene(reScene); } void HelloWorld::onEnter() { CCLayer::onEnter(); // 务必调用 CCLOG("HelloWorld::onEnter"); } void HelloWorld::onEnterTransitionDidFinish() { CCLayer::onEnterTransitionDidFinish(); // 务必调用 CCLOG("HelloWorld::onEnterTransitionDidFinish"); } void HelloWorld::onExit() { CCLayer::onExit(); CCLOG("HelloWorld::onExit"); }
// // TestLayer.h // #ifndef HelloWorld_TestLayer_h #define HelloWorld_TestLayer_h #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; class TestLayer : public cocos2d::CCLayer { public: virtual bool init(); static cocos2d::CCScene *scene(); CREATE_FUNC(TestLayer); // CCNode生命周期函数 virtual void onEnter(); virtual void onEnterTransitionDidFinish(); virtual void onExit(); }; #endif
// // TestLayer.cpp // #include "TestLayer.h" USING_NS_CC; CCScene *TestLayer::scene() { CCScene *scene = CCScene::create(); TestLayer *layer = TestLayer::create(); scene->addChild(layer); return scene; } bool TestLayer::init() { if (!CCLayer::init()) { return false; } CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSprite *pSpr = CCSprite::create("background2.png"); pSpr->setPosition(ccp(winSize.width*0.5,winSize.height*0.5)); this->addChild(pSpr); return true; } void TestLayer::onEnter() { CCLayer::onEnter(); CCLOG("TestLayer::onEnter"); } void TestLayer::onEnterTransitionDidFinish() { CCLayer::onEnterTransitionDidFinish(); CCLOG("TestLayer::onEnterTransitionDidFinish"); } void TestLayer::onExit() { CCLayer::onExit(); CCLOG("TestLayer::onExit"); }