游戏里面通常都会设计了很多场景,如果没有一个有效的管理方式,那将混乱不堪,Cocos2d-x里有一个CCDirector导演类,负责把控全场下发命令,所有的场景切换工作都必须经过导演类来执行,本案例学习场景的切换和场景切换特效,如图-1、图-2所示:
首先通过Xcode创建一个Cocos2d-x项目,命名为ReplaceScene,项目默认已经有一个HelloWorldScene场景了,在创建一个新的场景TestLayer,继承至CCLayer。
然后在TestLayer类中实现场景创建的方法scene,以及初始化方法init,在该方法中添加一个背景精灵和文字标签。
接下来在HelloWorldScene类中的初始化方法中添加一个背景精灵,以及一个文字菜单项。
从HelloWorldScene场景切换到TestLayer场景需要触发事件,实现菜单项的回调方法changeScene,该方法用于实现场景的切换和场景切换特效。
实现此案例需要按照如下步骤进行。
步骤一:创建场景TestLayer
首先通过Xcode创建一个Cocos2d-x项目,命名为ReplaceScene,项目默认已经有一个HelloWorldScene场景了,在创建一个新的场景TestLayer,继承至CCLayer。
然后在TestLayer类中实现场景创建的方法scene,代码如下所示:
- CCScene* TestLayer::scene()
- {
- CCScene *scene = CCScene::create();
- TestLayer *layer = TestLayer::create();
- scene->addChild(layer);
- return scene;
- }
接下来初始化方法init,在该方法中添加一个背景精灵和文字标签,并且给菜单项添加触发事件,代码如下所示:
- bool TestLayer::init()
- {
- if ( !CCLayer::init() )
- {
- return false;
- }
- CCLOG("TestLayer init");
- CCSprite * sp = CCSprite::create("background2.png");
- sp->setPosition(ccp(240,160));
- addChild(sp);
- CCLabelTTF* labelTTF =CCLabelTTF::create("返回", "Helvetica-Hald", 30);
- labelTTF->setColor(ccc3(255, 0, 0));
- CCMenuItemLabel * labelItem = CCMenuItemLabel::create(labelTTF, this, menu_selector(TestLayer::changeScene));
- CCMenu* menu= CCMenu::create(labelItem,NULL);
- addChild(menu);
- return true;
- }
最后实现changeScene方法,该方法用于实现场景的返回,代码如下所示:
- void TestLayer::changeScene(){
- CCDirector::sharedDirector()->popScene();
- }
步骤二:实现场景切换
接下来在HelloWorldScene类中的初始化方法中添加一个背景精灵,以及一个文字菜单项,代码如下所示:
- CCScene* HelloWorld::scene()
- {
- CCScene *scene = CCScene::create();
- HelloWorld *layer = HelloWorld::create();
- scene->addChild(layer);
- return scene;
- }
- bool HelloWorld::init()
- {
- if ( !CCLayer::init() )
- {
- return false;
- }
- CCSprite * sp = CCSprite::create("background1.png");
- sp->setPosition(ccp(240,160));
- addChild(sp);
- CCLabelTTF* labelTTF =CCLabelTTF::create("点击切换场景", "Helvetica-Hald", 30);
- labelTTF->setColor(ccc3(255, 0, 0));
- CCMenuItemLabel * labelItem = CCMenuItemLabel::create(labelTTF, this, menu_selector(HelloWorld::changeScene));
- CCMenu* menu= CCMenu::create(labelItem,NULL);
- addChild(menu);
- return true;
- }
从HelloWorldScene场景切换到TestLayer场景需要触发事件,实现菜单项的回调方法changeScene,该方法用于实现场景的切换和场景切换特效。
Cocosd-x提供了多种场景切换特效,常用的场景切换效果如下所示:
原场景缩小弹出,新场景放大弹入特效CCTransitionZoom;
逆时针切换特效CCTransitionProgressRadialCCW;
水平向右切换特效CCTransitionProgressHorizontal;
从里向外切换特效CCTransitionProgressOut;
原场景慢慢消失,新场景同时慢慢出现特效CCTransitionProgressCrossFade;
翻页特效CCTransitionPageForward;
方块切换左下角特效CCTransitionFadeTR;
从下向上的百叶窗特效CCTransitionFadeUp;
方块切换特效CCTransitionTurnOffTiles;
可以实现一段代码依次查看不同的场景切换效果,代码如下所示:
- void HelloWorld::changeScene(){
- CCTransitionScene* reScene =NULL;
- CCScene* s =TestLayer::scene();
- float t= 1.2f;
- switch (_index) {
- case _CCTransitionJumpZoom:
- reScene = CCTransitionJumpZoom::create(t,s) ;
- break;
- case _CCTransitionProgressRadialCCW:
- reScene = CCTransitionProgressRadialCCW::create(t,s);
- break;
- case _CCTransitionProgressRadialCW:
- reScene = CCTransitionProgressRadialCW::create(t,s);
- break;
- case _CCTransitionProgressHorizontal:
- reScene=CCTransitionProgressHorizontal::create(t, s);
- break;
- case _CCTransitionProgressVertical:
- reScene=CCTransitionProgressVertical::create(t, s);
- break;
- case _CCTransitionProgressInOut:
- reScene=CCTransitionProgressInOut::create(t, s);
- break;
- case _CCTransitionProgressOutIn:
- reScene=CCTransitionProgressOutIn::create(t, s);
- break;
- case _CCTransitionCrossFade:
- reScene =CCTransitionCrossFade::create(t, s);
- break;
- case _CCTransitionPageTurn:
- reScene =CCTransitionPageTurn::create(t, s, false);
- break;
- case _CCTransitionFadeTR:
- reScene = CCTransitionFadeTR::create(t, s);
- break;
- case _CCTransitionFadeBL:
- reScene = CCTransitionFadeBL::create(t, s);
- break;
- case _CCTransitionFadeUp:
- reScene = CCTransitionFadeUp::create(t, s);
- break;
- case _CCTransitionFadeDown:
- reScene = CCTransitionFadeDown::create(t, s);
- break;
- case _CCTransitionTurnOffTiles:
- reScene = CCTransitionTurnOffTiles::create(t, s);
- break;
- case _CCTransitionSplitRows:
- reScene = CCTransitionSplitRows::create(t, s);
- break;
- case _CCTransitionSplitCols:
- reScene = CCTransitionSplitCols::create(t, s);
- break;
- case _CCTransitionFade:
- reScene = CCTransitionFade::create(t, s,ccRED);
- break;
- case _CCTransitionFlipX:
- reScene=CCTransitionFlipX::create(t, s, kCCTransitionOrientationLeftOver);
- break;
- case _CCTransitionFlipY:
- reScene=CCTransitionFlipY::create(t, s, kCCTransitionOrientationUpOver);
- break;
- case _CCTransitionFlipAngular:
- reScene=CCTransitionFlipAngular::create(t, s,kCCTransitionOrientationLeftOver);
- break;
- case _CCTransitionZoomFlipX:
- reScene=CCTransitionZoomFlipX::create(t, s,kCCTransitionOrientationLeftOver);
- break;
- case _CCTransitionZoomFlipY:
- reScene=CCTransitionZoomFlipY::create(t, s,kCCTransitionOrientationUpOver);
- break;
- case _CCTransitionZoomFlipAngular:
- reScene = CCTransitionZoomFlipAngular::create(t, s,kCCTransitionOrientationLeftOver);
- break;
- case _CCTransitionShrinkGrow:
- reScene = CCTransitionShrinkGrow::create(t, s);
- break;
- case _CCTransitionRotoZoom:
- reScene = CCTransitionRotoZoom::create(t, s);
- break;
- case _CCTransitionMoveInL:
- reScene =CCTransitionMoveInL::create(t, s);
- break;
- case _CCTransitionMoveInR:
- reScene =CCTransitionMoveInR::create(t, s);
- break;
- case _CCTransitionMoveInT:
- reScene =CCTransitionMoveInT::create(t, s);
- break;
- case _CCTransitionMoveInB:
- reScene =CCTransitionMoveInB::create(t, s);
- break;
- case _CCTransitionSlideInL:
- reScene = CCTransitionSlideInL::create(t, s);
- break;
- case _CCTransitionSlideInR:
- reScene = CCTransitionSlideInR::create(t, s);
- break;
- case _CCTransitionSlideInT:
- reScene = CCTransitionSlideInT::create(t, s);
- break;
- case _CCTransitionSlideInB:
- reScene = CCTransitionSlideInB::create(t, s);
- break;
- default:
- break;
- }
- CCDirector::sharedDirector()->pushScene(reScene);
- int temp = int(_index);
- temp = ++temp == _CCTransitionCount ? _CCTransitionJumpZoom : temp;
- _index = (TransitionType)temp;
- }
本案例中,AppDelegate.cpp文件中的完整代码如下所示:
- #include "AppDelegate.h"
- #include "cocos2d.h"
- #include "SimpleAudioEngine.h"
- #include "HelloWorldScene.h"
- USING_NS_CC;
- using namespace CocosDenshion;
- AppDelegate::AppDelegate()
- {
- }
- AppDelegate::~AppDelegate()
- {
- }
- bool AppDelegate::applicationDidFinishLaunching()
- {
- // initialize director
- CCDirector *pDirector = CCDirector::sharedDirector();
- pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
- // turn on display FPS
- pDirector->setDisplayStats(true);
- // set FPS. the default value is 1.0/60 if you don't call this
- pDirector->setAnimationInterval(1.0 / 60);
- // create a scene. it's an autorelease object
- CCScene *pScene = HelloWorld::scene();
- // run
- pDirector->runWithScene(pScene);
- return true;
- }
- // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
- void AppDelegate::applicationDidEnterBackground()
- {
- CCDirector::sharedDirector()->stopAnimation();
- SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
- SimpleAudioEngine::sharedEngine()->pauseAllEffects();
- }
- // this function will be called when the app is active again
- void AppDelegate::applicationWillEnterForeground()
- {
- CCDirector::sharedDirector()->startAnimation();
- SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
- SimpleAudioEngine::sharedEngine()->resumeAllEffects();
- }
隐藏
本案例中,HelloWorld.h文件中的完整代码如下所示:
- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- #include "TestLayer.h"
- typedef enum{
- _CCTransitionJumpZoom,
- _CCTransitionProgressRadialCCW,
- _CCTransitionProgressRadialCW,
- _CCTransitionProgressHorizontal,
- _CCTransitionProgressVertical,
- _CCTransitionProgressInOut,
- _CCTransitionProgressOutIn,
- _CCTransitionCrossFade,
- _CCTransitionPageTurn,
- _CCTransitionFadeTR,
- _CCTransitionFadeBL,
- _CCTransitionFadeUp,
- _CCTransitionFadeDown,
- _CCTransitionTurnOffTiles,
- _CCTransitionSplitRows,
- _CCTransitionSplitCols,
- _CCTransitionFade,
- _CCTransitionFlipX,
- _CCTransitionFlipY,
- _CCTransitionFlipAngular,
- _CCTransitionZoomFlipX,
- _CCTransitionZoomFlipY,
- _CCTransitionZoomFlipAngular,
- _CCTransitionShrinkGrow,
- _CCTransitionRotoZoom,
- _CCTransitionMoveInL,
- _CCTransitionMoveInR,
- _CCTransitionMoveInT,
- _CCTransitionMoveInB,
- _CCTransitionSlideInL,
- _CCTransitionSlideInR,
- _CCTransitionSlideInT,
- _CCTransitionSlideInB,
- _CCTransitionCount,
- }TransitionType;
- class HelloWorld : public cocos2d::CCLayer
- {
- public:
- // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
- virtual bool init();
- // there's no 'id' in cpp, so we recommend to return the class instance pointer
- static cocos2d::CCScene* scene();
- // preprocessor macro for "static create()" constructor ( node() deprecated )
- CREATE_FUNC(HelloWorld);
- void changeScene();
- private:
- TransitionType _index;
- };
隐藏
本案例中,HelloWorld.cpp文件中的完整代码如下所示:
- #include "HelloWorldScene.h"
- #include "SimpleAudioEngine.h"
- using namespace cocos2d;
- using namespace CocosDenshion;
- CCScene* HelloWorld::scene()
- {
- CCScene *scene = CCScene::create();
- HelloWorld *layer = HelloWorld::create();
- scene->addChild(layer);
- return scene;
- }
- // on "init" you need to initialize your instance
- bool HelloWorld::init()
- {
- if ( !CCLayer::init() )
- {
- return false;
- }
- CCSprite * sp = CCSprite::create("background1.png");
- sp->setPosition(ccp(240,160));
- addChild(sp);
- CCLabelTTF* labelTTF =CCLabelTTF::create("点击切换场景", "Helvetica-Hald", 30);
- labelTTF->setColor(ccc3(255, 0, 0));
- 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;
- switch (_index) {
- case _CCTransitionJumpZoom:
- reScene = CCTransitionJumpZoom::create(t,s) ;
- break;
- case _CCTransitionProgressRadialCCW:
- reScene = CCTransitionProgressRadialCCW::create(t,s);
- break;
- case _CCTransitionProgressRadialCW:
- reScene = CCTransitionProgressRadialCW::create(t,s);
- break;
- case _CCTransitionProgressHorizontal:
- reScene=CCTransitionProgressHorizontal::create(t, s);
- break;
- case _CCTransitionProgressVertical:
- reScene=CCTransitionProgressVertical::create(t, s);
- break;
- case _CCTransitionProgressInOut:
- reScene=CCTransitionProgressInOut::create(t, s);
- break;
- case _CCTransitionProgressOutIn:
- reScene=CCTransitionProgressOutIn::create(t, s);
- break;
- case _CCTransitionCrossFade:
- reScene =CCTransitionCrossFade::create(t, s);
- break;
- case _CCTransitionPageTurn:
- reScene =CCTransitionPageTurn::create(t, s, false);
- break;
- case _CCTransitionFadeTR:
- reScene = CCTransitionFadeTR::create(t, s);
- break;
- case _CCTransitionFadeBL:
- reScene = CCTransitionFadeBL::create(t, s);
- break;
- case _CCTransitionFadeUp:
- reScene = CCTransitionFadeUp::create(t, s);
- break;
- case _CCTransitionFadeDown:
- reScene = CCTransitionFadeDown::create(t, s);
- break;
- case _CCTransitionTurnOffTiles:
- reScene = CCTransitionTurnOffTiles::create(t, s);
- break;
- case _CCTransitionSplitRows:
- reScene = CCTransitionSplitRows::create(t, s);
- break;
- case _CCTransitionSplitCols:
- reScene = CCTransitionSplitCols::create(t, s);
- break;
- case _CCTransitionFade:
- reScene = CCTransitionFade::create(t, s,ccRED);
- break;
- case _CCTransitionFlipX:
- reScene=CCTransitionFlipX::create(t, s, kCCTransitionOrientationLeftOver);
- break;
- case _CCTransitionFlipY:
- reScene=CCTransitionFlipY::create(t, s, kCCTransitionOrientationUpOver);
- break;
- case _CCTransitionFlipAngular:
- reScene=CCTransitionFlipAngular::create(t, s,kCCTransitionOrientationLeftOver);
- break;
- case _CCTransitionZoomFlipX:
- reScene=CCTransitionZoomFlipX::create(t, s,kCCTransitionOrientationLeftOver);
- break;
- case _CCTransitionZoomFlipY:
- reScene=CCTransitionZoomFlipY::create(t, s,kCCTransitionOrientationUpOver);
- break;
- case _CCTransitionZoomFlipAngular:
- reScene = CCTransitionZoomFlipAngular::create(t, s,kCCTransitionOrientationLeftOver);
- break;
- case _CCTransitionShrinkGrow:
- reScene = CCTransitionShrinkGrow::create(t, s);
- break;
- case _CCTransitionRotoZoom:
- reScene = CCTransitionRotoZoom::create(t, s);
- break;
- case _CCTransitionMoveInL:
- reScene =CCTransitionMoveInL::create(t, s);
- break;
- case _CCTransitionMoveInR:
- reScene =CCTransitionMoveInR::create(t, s);
- break;
- case _CCTransitionMoveInT:
- reScene =CCTransitionMoveInT::create(t, s);
- break;
- case _CCTransitionMoveInB:
- reScene =CCTransitionMoveInB::create(t, s);
- break;
- case _CCTransitionSlideInL:
- reScene = CCTransitionSlideInL::create(t, s);
- break;
- case _CCTransitionSlideInR:
- reScene = CCTransitionSlideInR::create(t, s);
- break;
- case _CCTransitionSlideInT:
- reScene = CCTransitionSlideInT::create(t, s);
- break;
- case _CCTransitionSlideInB:
- reScene = CCTransitionSlideInB::create(t, s);
- break;
- default:
- break;
- }
- CCDirector::sharedDirector()->pushScene(reScene);
- int temp = int(_index);
- temp = ++temp == _CCTransitionCount ? _CCTransitionJumpZoom : temp;
- _index = (TransitionType)temp;
- }
隐藏
本案例中,TestLayer.h文件中的完整代码如下所示:
- #ifndef __day_04_02_ReplaceScene__TestLayer__
- #define __day_04_02_ReplaceScene__TestLayer__
- #include <iostream>
- #include "cocos2d.h"
- class TestLayer : public cocos2d::CCLayer
- {
- public:
- virtual bool init();
- static cocos2d::CCScene* scene();
- CREATE_FUNC(TestLayer);
- void changeScene();
- };
- #endif /* defined(__day_04_02_ReplaceScene__TestLayer__) */
隐藏
本案例中,TestLayer.cpp文件中的完整代码如下所示:
- #include "TestLayer.h"
- #include "HelloWorldScene.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;
- }
- CCLOG("TestLayer init");
- CCSprite * sp = CCSprite::create("background2.png");
- sp->setPosition(ccp(240,160));
- addChild(sp);
- CCLabelTTF* labelTTF =CCLabelTTF::create("返回", "Helvetica-Hald", 30);
- labelTTF->setColor(ccc3(255, 0, 0));
- CCMenuItemLabel * labelItem = CCMenuItemLabel::create(labelTTF, this, menu_selector(TestLayer::changeScene));
- CCMenu* menu= CCMenu::create(labelItem,NULL);
- addChild(menu);
- return true;
- }
- void TestLayer::changeScene(){
- CCDirector::sharedDirector()->popScene();
- }