cocos2d-x 2.x 场景切换、事件处理

1 场景切换

1.1 问题

游戏里面通常都会设计了很多场景,如果没有一个有效的管理方式,那将混乱不堪,Cocos2d-x里有一个CCDirector导演类,负责把控全场下发命令,所有的场景切换工作都必须经过导演类来执行,本案例学习场景的切换和场景切换特效,如图-1、图-2所示:

cocos2d-x 2.x 场景切换、事件处理_第1张图片

图-1

cocos2d-x 2.x 场景切换、事件处理_第2张图片

图-2

1.2 方案

首先通过Xcode创建一个Cocos2d-x项目,命名为ReplaceScene,项目默认已经有一个HelloWorldScene场景了,在创建一个新的场景TestLayer,继承至CCLayer。

然后在TestLayer类中实现场景创建的方法scene,以及初始化方法init,在该方法中添加一个背景精灵和文字标签。

接下来在HelloWorldScene类中的初始化方法中添加一个背景精灵,以及一个文字菜单项。

从HelloWorldScene场景切换到TestLayer场景需要触发事件,实现菜单项的回调方法changeScene,该方法用于实现场景的切换和场景切换特效。

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:创建场景TestLayer

首先通过Xcode创建一个Cocos2d-x项目,命名为ReplaceScene,项目默认已经有一个HelloWorldScene场景了,在创建一个新的场景TestLayer,继承至CCLayer。

然后在TestLayer类中实现场景创建的方法scene,代码如下所示:


      
      
      
      
  1. CCScene* TestLayer::scene()
  2. {
  3. CCScene *scene = CCScene::create();
  4. TestLayer *layer = TestLayer::create();
  5. scene->addChild(layer);
  6. return scene;
  7. }

接下来初始化方法init,在该方法中添加一个背景精灵和文字标签,并且给菜单项添加触发事件,代码如下所示:


      
      
      
      
  1. bool TestLayer::init()
  2. {
  3. if ( !CCLayer::init() )
  4. {
  5. return false;
  6. }
  7. CCLOG("TestLayer init");
  8. CCSprite * sp = CCSprite::create("background2.png");
  9. sp->setPosition(ccp(240,160));
  10. addChild(sp);
  11. CCLabelTTF* labelTTF =CCLabelTTF::create("返回", "Helvetica-Hald", 30);
  12. labelTTF->setColor(ccc3(255, 0, 0));
  13. CCMenuItemLabel * labelItem = CCMenuItemLabel::create(labelTTF, this, menu_selector(TestLayer::changeScene));
  14. CCMenu* menu= CCMenu::create(labelItem,NULL);
  15. addChild(menu);
  16. return true;
  17. }

最后实现changeScene方法,该方法用于实现场景的返回,代码如下所示:


      
      
      
      
  1. void TestLayer::changeScene(){
  2. CCDirector::sharedDirector()->popScene();
  3. }

步骤二:实现场景切换

接下来在HelloWorldScene类中的初始化方法中添加一个背景精灵,以及一个文字菜单项,代码如下所示:


      
      
      
      
  1. CCScene* HelloWorld::scene()
  2. {
  3. CCScene *scene = CCScene::create();
  4. HelloWorld *layer = HelloWorld::create();
  5. scene->addChild(layer);
  6. return scene;
  7. }
  8. bool HelloWorld::init()
  9. {
  10. if ( !CCLayer::init() )
  11. {
  12. return false;
  13. }
  14. CCSprite * sp = CCSprite::create("background1.png");
  15. sp->setPosition(ccp(240,160));
  16. addChild(sp);
  17. CCLabelTTF* labelTTF =CCLabelTTF::create("点击切换场景", "Helvetica-Hald", 30);
  18. labelTTF->setColor(ccc3(255, 0, 0));
  19. CCMenuItemLabel * labelItem = CCMenuItemLabel::create(labelTTF, this, menu_selector(HelloWorld::changeScene));
  20. CCMenu* menu= CCMenu::create(labelItem,NULL);
  21. addChild(menu);
  22. return true;
  23. }

从HelloWorldScene场景切换到TestLayer场景需要触发事件,实现菜单项的回调方法changeScene,该方法用于实现场景的切换和场景切换特效。

Cocosd-x提供了多种场景切换特效,常用的场景切换效果如下所示:

原场景缩小弹出,新场景放大弹入特效CCTransitionZoom;

逆时针切换特效CCTransitionProgressRadialCCW;

水平向右切换特效CCTransitionProgressHorizontal;

从里向外切换特效CCTransitionProgressOut;

原场景慢慢消失,新场景同时慢慢出现特效CCTransitionProgressCrossFade;

翻页特效CCTransitionPageForward;

方块切换左下角特效CCTransitionFadeTR;

从下向上的百叶窗特效CCTransitionFadeUp;

方块切换特效CCTransitionTurnOffTiles;

可以实现一段代码依次查看不同的场景切换效果,代码如下所示:


      
      
      
      
  1. void HelloWorld::changeScene(){
  2. CCTransitionScene* reScene =NULL;
  3. CCScene* s =TestLayer::scene();
  4. float t= 1.2f;
  5. switch (_index) {
  6. case _CCTransitionJumpZoom:
  7. reScene = CCTransitionJumpZoom::create(t,s) ;
  8. break;
  9. case _CCTransitionProgressRadialCCW:
  10. reScene = CCTransitionProgressRadialCCW::create(t,s);
  11. break;
  12. case _CCTransitionProgressRadialCW:
  13. reScene = CCTransitionProgressRadialCW::create(t,s);
  14. break;
  15. case _CCTransitionProgressHorizontal:
  16. reScene=CCTransitionProgressHorizontal::create(t, s);
  17. break;
  18. case _CCTransitionProgressVertical:
  19. reScene=CCTransitionProgressVertical::create(t, s);
  20. break;
  21. case _CCTransitionProgressInOut:
  22. reScene=CCTransitionProgressInOut::create(t, s);
  23. break;
  24. case _CCTransitionProgressOutIn:
  25. reScene=CCTransitionProgressOutIn::create(t, s);
  26. break;
  27. case _CCTransitionCrossFade:
  28. reScene =CCTransitionCrossFade::create(t, s);
  29. break;
  30. case _CCTransitionPageTurn:
  31. reScene =CCTransitionPageTurn::create(t, s, false);
  32. break;
  33. case _CCTransitionFadeTR:
  34. reScene = CCTransitionFadeTR::create(t, s);
  35. break;
  36. case _CCTransitionFadeBL:
  37. reScene = CCTransitionFadeBL::create(t, s);
  38. break;
  39. case _CCTransitionFadeUp:
  40. reScene = CCTransitionFadeUp::create(t, s);
  41. break;
  42. case _CCTransitionFadeDown:
  43. reScene = CCTransitionFadeDown::create(t, s);
  44. break;
  45. case _CCTransitionTurnOffTiles:
  46. reScene = CCTransitionTurnOffTiles::create(t, s);
  47. break;
  48. case _CCTransitionSplitRows:
  49. reScene = CCTransitionSplitRows::create(t, s);
  50. break;
  51. case _CCTransitionSplitCols:
  52. reScene = CCTransitionSplitCols::create(t, s);
  53. break;
  54. case _CCTransitionFade:
  55. reScene = CCTransitionFade::create(t, s,ccRED);
  56. break;
  57. case _CCTransitionFlipX:
  58. reScene=CCTransitionFlipX::create(t, s, kCCTransitionOrientationLeftOver);
  59. break;
  60. case _CCTransitionFlipY:
  61. reScene=CCTransitionFlipY::create(t, s, kCCTransitionOrientationUpOver);
  62. break;
  63. case _CCTransitionFlipAngular:
  64. reScene=CCTransitionFlipAngular::create(t, s,kCCTransitionOrientationLeftOver);
  65. break;
  66. case _CCTransitionZoomFlipX:
  67. reScene=CCTransitionZoomFlipX::create(t, s,kCCTransitionOrientationLeftOver);
  68. break;
  69. case _CCTransitionZoomFlipY:
  70. reScene=CCTransitionZoomFlipY::create(t, s,kCCTransitionOrientationUpOver);
  71. break;
  72. case _CCTransitionZoomFlipAngular:
  73. reScene = CCTransitionZoomFlipAngular::create(t, s,kCCTransitionOrientationLeftOver);
  74. break;
  75. case _CCTransitionShrinkGrow:
  76. reScene = CCTransitionShrinkGrow::create(t, s);
  77. break;
  78. case _CCTransitionRotoZoom:
  79. reScene = CCTransitionRotoZoom::create(t, s);
  80. break;
  81. case _CCTransitionMoveInL:
  82. reScene =CCTransitionMoveInL::create(t, s);
  83. break;
  84. case _CCTransitionMoveInR:
  85. reScene =CCTransitionMoveInR::create(t, s);
  86. break;
  87. case _CCTransitionMoveInT:
  88. reScene =CCTransitionMoveInT::create(t, s);
  89. break;
  90. case _CCTransitionMoveInB:
  91. reScene =CCTransitionMoveInB::create(t, s);
  92. break;
  93. case _CCTransitionSlideInL:
  94. reScene = CCTransitionSlideInL::create(t, s);
  95. break;
  96. case _CCTransitionSlideInR:
  97. reScene = CCTransitionSlideInR::create(t, s);
  98. break;
  99. case _CCTransitionSlideInT:
  100. reScene = CCTransitionSlideInT::create(t, s);
  101. break;
  102. case _CCTransitionSlideInB:
  103. reScene = CCTransitionSlideInB::create(t, s);
  104. break;
  105. default:
  106. break;
  107. }
  108. CCDirector::sharedDirector()->pushScene(reScene);
  109. int temp = int(_index);
  110. temp = ++temp == _CCTransitionCount ? _CCTransitionJumpZoom : temp;
  111. _index = (TransitionType)temp;
  112. }

1.4 完整代码

本案例中,AppDelegate.cpp文件中的完整代码如下所示:


      
      
      
      
  1. #include "AppDelegate.h"
  2. #include "cocos2d.h"
  3. #include "SimpleAudioEngine.h"
  4. #include "HelloWorldScene.h"
  5. USING_NS_CC;
  6. using namespace CocosDenshion;
  7. AppDelegate::AppDelegate()
  8. {
  9. }
  10. AppDelegate::~AppDelegate()
  11. {
  12. }
  13. bool AppDelegate::applicationDidFinishLaunching()
  14. {
  15. // initialize director
  16. CCDirector *pDirector = CCDirector::sharedDirector();
  17. pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
  18. // turn on display FPS
  19. pDirector->setDisplayStats(true);
  20. // set FPS. the default value is 1.0/60 if you don't call this
  21. pDirector->setAnimationInterval(1.0 / 60);
  22. // create a scene. it's an autorelease object
  23. CCScene *pScene = HelloWorld::scene();
  24. // run
  25. pDirector->runWithScene(pScene);
  26. return true;
  27. }
  28. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
  29. void AppDelegate::applicationDidEnterBackground()
  30. {
  31. CCDirector::sharedDirector()->stopAnimation();
  32. SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
  33. SimpleAudioEngine::sharedEngine()->pauseAllEffects();
  34. }
  35. // this function will be called when the app is active again
  36. void AppDelegate::applicationWillEnterForeground()
  37. {
  38. CCDirector::sharedDirector()->startAnimation();
  39. SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
  40. SimpleAudioEngine::sharedEngine()->resumeAllEffects();
  41. }
隐藏

本案例中,HelloWorld.h文件中的完整代码如下所示:


      
      
      
      
  1. #ifndef __HELLOWORLD_SCENE_H__
  2. #define __HELLOWORLD_SCENE_H__
  3. #include "cocos2d.h"
  4. #include "TestLayer.h"
  5. typedef enum{
  6. _CCTransitionJumpZoom,
  7. _CCTransitionProgressRadialCCW,
  8. _CCTransitionProgressRadialCW,
  9. _CCTransitionProgressHorizontal,
  10. _CCTransitionProgressVertical,
  11. _CCTransitionProgressInOut,
  12. _CCTransitionProgressOutIn,
  13. _CCTransitionCrossFade,
  14. _CCTransitionPageTurn,
  15. _CCTransitionFadeTR,
  16. _CCTransitionFadeBL,
  17. _CCTransitionFadeUp,
  18. _CCTransitionFadeDown,
  19. _CCTransitionTurnOffTiles,
  20. _CCTransitionSplitRows,
  21. _CCTransitionSplitCols,
  22. _CCTransitionFade,
  23. _CCTransitionFlipX,
  24. _CCTransitionFlipY,
  25. _CCTransitionFlipAngular,
  26. _CCTransitionZoomFlipX,
  27. _CCTransitionZoomFlipY,
  28. _CCTransitionZoomFlipAngular,
  29. _CCTransitionShrinkGrow,
  30. _CCTransitionRotoZoom,
  31. _CCTransitionMoveInL,
  32. _CCTransitionMoveInR,
  33. _CCTransitionMoveInT,
  34. _CCTransitionMoveInB,
  35. _CCTransitionSlideInL,
  36. _CCTransitionSlideInR,
  37. _CCTransitionSlideInT,
  38. _CCTransitionSlideInB,
  39. _CCTransitionCount,
  40. }TransitionType;
  41. class HelloWorld : public cocos2d::CCLayer
  42. {
  43. public:
  44. // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
  45. virtual bool init();
  46. // there's no 'id' in cpp, so we recommend to return the class instance pointer
  47. static cocos2d::CCScene* scene();
  48. // preprocessor macro for "static create()" constructor ( node() deprecated )
  49. CREATE_FUNC(HelloWorld);
  50. void changeScene();
  51. private:
  52. TransitionType _index;
  53. };
隐藏

本案例中,HelloWorld.cpp文件中的完整代码如下所示:


      
      
      
      
  1. #include "HelloWorldScene.h"
  2. #include "SimpleAudioEngine.h"
  3. using namespace cocos2d;
  4. using namespace CocosDenshion;
  5. CCScene* HelloWorld::scene()
  6. {
  7. CCScene *scene = CCScene::create();
  8. HelloWorld *layer = HelloWorld::create();
  9. scene->addChild(layer);
  10. return scene;
  11. }
  12. // on "init" you need to initialize your instance
  13. bool HelloWorld::init()
  14. {
  15. if ( !CCLayer::init() )
  16. {
  17. return false;
  18. }
  19. CCSprite * sp = CCSprite::create("background1.png");
  20. sp->setPosition(ccp(240,160));
  21. addChild(sp);
  22. CCLabelTTF* labelTTF =CCLabelTTF::create("点击切换场景", "Helvetica-Hald", 30);
  23. labelTTF->setColor(ccc3(255, 0, 0));
  24. CCMenuItemLabel * labelItem = CCMenuItemLabel::create(labelTTF, this, menu_selector(HelloWorld::changeScene));
  25. CCMenu* menu= CCMenu::create(labelItem,NULL);
  26. addChild(menu);
  27. return true;
  28. }
  29. void HelloWorld::changeScene(){
  30. CCTransitionScene* reScene =NULL;
  31. CCScene* s =TestLayer::scene();
  32. float t= 1.2f;
  33. switch (_index) {
  34. case _CCTransitionJumpZoom:
  35. reScene = CCTransitionJumpZoom::create(t,s) ;
  36. break;
  37. case _CCTransitionProgressRadialCCW:
  38. reScene = CCTransitionProgressRadialCCW::create(t,s);
  39. break;
  40. case _CCTransitionProgressRadialCW:
  41. reScene = CCTransitionProgressRadialCW::create(t,s);
  42. break;
  43. case _CCTransitionProgressHorizontal:
  44. reScene=CCTransitionProgressHorizontal::create(t, s);
  45. break;
  46. case _CCTransitionProgressVertical:
  47. reScene=CCTransitionProgressVertical::create(t, s);
  48. break;
  49. case _CCTransitionProgressInOut:
  50. reScene=CCTransitionProgressInOut::create(t, s);
  51. break;
  52. case _CCTransitionProgressOutIn:
  53. reScene=CCTransitionProgressOutIn::create(t, s);
  54. break;
  55. case _CCTransitionCrossFade:
  56. reScene =CCTransitionCrossFade::create(t, s);
  57. break;
  58. case _CCTransitionPageTurn:
  59. reScene =CCTransitionPageTurn::create(t, s, false);
  60. break;
  61. case _CCTransitionFadeTR:
  62. reScene = CCTransitionFadeTR::create(t, s);
  63. break;
  64. case _CCTransitionFadeBL:
  65. reScene = CCTransitionFadeBL::create(t, s);
  66. break;
  67. case _CCTransitionFadeUp:
  68. reScene = CCTransitionFadeUp::create(t, s);
  69. break;
  70. case _CCTransitionFadeDown:
  71. reScene = CCTransitionFadeDown::create(t, s);
  72. break;
  73. case _CCTransitionTurnOffTiles:
  74. reScene = CCTransitionTurnOffTiles::create(t, s);
  75. break;
  76. case _CCTransitionSplitRows:
  77. reScene = CCTransitionSplitRows::create(t, s);
  78. break;
  79. case _CCTransitionSplitCols:
  80. reScene = CCTransitionSplitCols::create(t, s);
  81. break;
  82. case _CCTransitionFade:
  83. reScene = CCTransitionFade::create(t, s,ccRED);
  84. break;
  85. case _CCTransitionFlipX:
  86. reScene=CCTransitionFlipX::create(t, s, kCCTransitionOrientationLeftOver);
  87. break;
  88. case _CCTransitionFlipY:
  89. reScene=CCTransitionFlipY::create(t, s, kCCTransitionOrientationUpOver);
  90. break;
  91. case _CCTransitionFlipAngular:
  92. reScene=CCTransitionFlipAngular::create(t, s,kCCTransitionOrientationLeftOver);
  93. break;
  94. case _CCTransitionZoomFlipX:
  95. reScene=CCTransitionZoomFlipX::create(t, s,kCCTransitionOrientationLeftOver);
  96. break;
  97. case _CCTransitionZoomFlipY:
  98. reScene=CCTransitionZoomFlipY::create(t, s,kCCTransitionOrientationUpOver);
  99. break;
  100. case _CCTransitionZoomFlipAngular:
  101. reScene = CCTransitionZoomFlipAngular::create(t, s,kCCTransitionOrientationLeftOver);
  102. break;
  103. case _CCTransitionShrinkGrow:
  104. reScene = CCTransitionShrinkGrow::create(t, s);
  105. break;
  106. case _CCTransitionRotoZoom:
  107. reScene = CCTransitionRotoZoom::create(t, s);
  108. break;
  109. case _CCTransitionMoveInL:
  110. reScene =CCTransitionMoveInL::create(t, s);
  111. break;
  112. case _CCTransitionMoveInR:
  113. reScene =CCTransitionMoveInR::create(t, s);
  114. break;
  115. case _CCTransitionMoveInT:
  116. reScene =CCTransitionMoveInT::create(t, s);
  117. break;
  118. case _CCTransitionMoveInB:
  119. reScene =CCTransitionMoveInB::create(t, s);
  120. break;
  121. case _CCTransitionSlideInL:
  122. reScene = CCTransitionSlideInL::create(t, s);
  123. break;
  124. case _CCTransitionSlideInR:
  125. reScene = CCTransitionSlideInR::create(t, s);
  126. break;
  127. case _CCTransitionSlideInT:
  128. reScene = CCTransitionSlideInT::create(t, s);
  129. break;
  130. case _CCTransitionSlideInB:
  131. reScene = CCTransitionSlideInB::create(t, s);
  132. break;
  133. default:
  134. break;
  135. }
  136. CCDirector::sharedDirector()->pushScene(reScene);
  137. int temp = int(_index);
  138. temp = ++temp == _CCTransitionCount ? _CCTransitionJumpZoom : temp;
  139. _index = (TransitionType)temp;
  140. }
隐藏

本案例中,TestLayer.h文件中的完整代码如下所示:


      
      
      
      
  1. #ifndef __day_04_02_ReplaceScene__TestLayer__
  2. #define __day_04_02_ReplaceScene__TestLayer__
  3. #include <iostream>
  4. #include "cocos2d.h"
  5. class TestLayer : public cocos2d::CCLayer
  6. {
  7. public:
  8. virtual bool init();
  9. static cocos2d::CCScene* scene();
  10. CREATE_FUNC(TestLayer);
  11. void changeScene();
  12. };
  13. #endif /* defined(__day_04_02_ReplaceScene__TestLayer__) */
隐藏

本案例中,TestLayer.cpp文件中的完整代码如下所示:


      
      
      
      
  1. #include "TestLayer.h"
  2. #include "HelloWorldScene.h"
  3. USING_NS_CC;
  4. CCScene* TestLayer::scene()
  5. {
  6. CCScene *scene = CCScene::create();
  7. TestLayer *layer = TestLayer::create();
  8. scene->addChild(layer);
  9. return scene;
  10. }
  11. bool TestLayer::init()
  12. {
  13. if ( !CCLayer::init() )
  14. {
  15. return false;
  16. }
  17. CCLOG("TestLayer init");
  18. CCSprite * sp = CCSprite::create("background2.png");
  19. sp->setPosition(ccp(240,160));
  20. addChild(sp);
  21. CCLabelTTF* labelTTF =CCLabelTTF::create("返回", "Helvetica-Hald", 30);
  22. labelTTF->setColor(ccc3(255, 0, 0));
  23. CCMenuItemLabel * labelItem = CCMenuItemLabel::create(labelTTF, this, menu_selector(TestLayer::changeScene));
  24. CCMenu* menu= CCMenu::create(labelItem,NULL);
  25. addChild(menu);
  26. return true;
  27. }
  28. void TestLayer::changeScene(){
  29. CCDirector::sharedDirector()->popScene();
  30. }

你可能感兴趣的:(手游开发)