【Cocos2dx】新建场景、场景的切换、设置启动场景与菜单的新建

这是Cocos2dx最简单的部分。主要是体现对场景的操作,其实这东西就是Flash的舞台,安卓的Activity,WIN32窗体程序的Framework窗体,网页的body,反正就是对那个容纳各种东西的大容器进行操作,爱怎么叫就怎么叫。

百牛信息技术bainiu.ltd整理发布于博客园

用一个例子说明这个问题,将会做出如下的效果,在官方提供的Helloworld加一个场景Scene1,Scene1里面就摆一个可以切回Helloworld的按钮,同时设置这个Scene1为启动程序(游戏)的初始场景。同时对原本Helloworld场景的关闭按钮进行改造,原本关闭程序调整为切换到Scene1。两个场景切换有动画效果,当然这是Cocos2dx本来就自带的。

1、首先,与《【Cocos2dx】Windows平台下Cocos2dx 2.x的下载、安装、配置,打造自己的Helloworld》(点击打开链接)中一样,利用(cocos2d-x-2.2.6安装目录). oolsproject-creator下的create_project.py,输入:

[plain] view plain copy
 
  1. create_project.py -project Scene -package test.scene -language cpp    


在(cocos2d-x-2.2.6安装目录).project下得到一个Scene文件夹,打开其中的proj.win32中的HelloCpp.sln利用vs2010进行编辑。

2、上来直接新建场景Scene1,如下图,对HelloCpp下的Classes文件夹中,新建两个项一个Scene1.h,另一个为Scene1.cpp。

这里千万要注意的时,记得把这个文件创建在(工程目录).Classes文件中,默认是坑爹的proj.win32,如果不创建在.Classes文件夹中,你创建的文件夹,无法与原来就存在的文件使用include命令相互交互……

3、对Scene1.h编写如下的代码,技巧是模仿原来就存在的HelloWorldScene.h声明一个场景

[cpp] view plain copy
 
  1. #ifndef __SCENE1_H__  
  2. #define __SCENE1_H__  
  3.   
  4. #include "cocos2d.h"  
  5.   
  6. class Scene1:public cocos2d::CCLayer  
  7. {  
  8. public:  
  9.     // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone  
  10.     virtual bool init();    
  11.   
  12.     // there's no 'id' in cpp, so we recommend returning the class instance pointer  
  13.     static cocos2d::CCScene* scene();  
  14.      
  15.     void menuGoToHelloworld(CCObject* pSender);//声明场景切换的按钮的回调(执行)函数  
  16.   
  17.     //原本为HelloWorld这里改成Scene1  
  18.     CREATE_FUNC(Scene1);  
  19. };  
  20.   
  21. #endif   
  22.   
  23. //这里仿造HelloWorldScene.h这个文件进行修改,把原本为HelloWorld都改成Scene1,此文件主要是场景的声明、按钮函数的声明  

4、之后,对Scene1.cpp编写如下的代码,同样是模仿原来就存在的HelloWorldScene.cpp的关闭按钮,及其回调函数,也就是执行函数。

[cpp] view plain copy
 
  1. #include "HelloWorldScene.h"//由于要切换回Helloworld这个场景,因此要声明这个函数  
  2. #include "Scene1.h"  
  3.   
  4. USING_NS_CC;  
  5. //声明部分,依旧仿造HelloWorldScene.h进行修改  
  6. CCScene* Scene1::scene()  
  7. {  
  8.     // 'scene' is an autorelease object  
  9.     CCScene *scene = CCScene::create();  
  10.   
  11.     // 'layer' is an autorelease object  
  12.     Scene1 *layer = Scene1::create();  
  13.   
  14.     // add layer as a child to scene  
  15.     scene->addChild(layer);  
  16.   
  17.     // return the scene  
  18.     return scene;  
  19. }  
  20.   
  21. //精华部分,场景组件的放置  
  22. bool Scene1::init()  
  23. {  
  24.     //声明位置组件,主要是为了下方确定位置的setPosition函数中ccp,origin等可以跨平台确定函数的组件可用  
  25.     CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();  
  26.     CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();  
  27.   
  28.     //声明一个按钮  
  29.     CCMenuItemImage *pCloseItem = CCMenuItemImage::create(  
  30.         "CloseNormal.png",//正常状态的图片,系统自带的  
  31.         "CloseSelected.png",//被点击的图片  
  32.         this,  
  33.         menu_selector(Scene1::menuGoToHelloworld));//声明按钮的回调(执行)函数,头文件已经声明过这个函数  
  34.   
  35.     //按钮的位置  
  36.     pCloseItem->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height/2));  
  37.   
  38.     //摆放按钮的固有实现部分,HelloWorldScene.cpp复制过来的,什么意思不用管  
  39.     // create menu, it's an autorelease object  
  40.     CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);  
  41.     pMenu->setPosition(CCPointZero);  
  42.     this->addChild(pMenu);  
  43.   
  44.   
  45.     return true;  
  46. }  
  47.   
  48. //按钮的回调(执行)函数的实现  
  49. void Scene1::menuGoToHelloworld(CCObject* pSender)  
  50. {  
  51. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)  
  52.     CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");  
  53. #else  
  54.     //核心在这句话,其余都是HelloWorldScene.cpp复制过来的,什么意思不用管,把原本的end()方法,改成切换场景replaceScene()方法。  
  55.     //CCTransitionMoveInL为左进入特效,0.4f为耗时,越少越快,可以为3.0f等,HelloWorld::scene()就是要切换到的场景  
  56.     CCDirector::sharedDirector()->replaceScene(CCTransitionMoveInL::create(0.4f,HelloWorld::scene()));  
  57. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  58.     exit(0);  
  59. #endif  
  60. #endif  
  61. }  

5、之后,同理,把在HelloworldScene.cpp中引入Scene1.h这个头文件。同时修改一个其关闭按钮的回调函数,在第86行,从原本的关闭,改为渐变切换特效。具体如下:

[cpp] view plain copy
 
  1. #include "HelloWorldScene.h"  
  2. #include "Scene1.h"//引入要切换的场景  
  3.   
  4. USING_NS_CC;  
  5.   
  6. CCScene* HelloWorld::scene()  
  7. {  
  8.     // 'scene' is an autorelease object  
  9.     CCScene *scene = CCScene::create();  
  10.       
  11.     // 'layer' is an autorelease object  
  12.     HelloWorld *layer = HelloWorld::create();  
  13.   
  14.     // add layer as a child to scene  
  15.     scene->addChild(layer);  
  16.   
  17.     // return the scene  
  18.     return scene;  
  19. }  
  20.   
  21. // on "init" you need to initialize your instance  
  22. bool HelloWorld::init()  
  23. {  
  24.     //////////////////////////////  
  25.     // 1. super init first  
  26.     if ( !CCLayer::init() )  
  27.     {  
  28.         return false;  
  29.     }  
  30.       
  31.     CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();  
  32.     CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();  
  33.   
  34.     /////////////////////////////  
  35.     // 2. add a menu item with "X" image, which is clicked to quit the program  
  36.     //    you may modify it.  
  37.   
  38.     // add a "close" icon to exit the progress. it's an autorelease object  
  39.     CCMenuItemImage *pCloseItem = CCMenuItemImage::create(  
  40.                                         "CloseNormal.png",  
  41.                                         "CloseSelected.png",  
  42.                                         this,  
  43.                                         menu_selector(HelloWorld::menuCloseCallback));  
  44.       
  45.     pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,  
  46.                                 origin.y + pCloseItem->getContentSize().height/2));  
  47.   
  48.     // create menu, it's an autorelease object  
  49.     CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);  
  50.     pMenu->setPosition(CCPointZero);  
  51.     this->addChild(pMenu, 1);  
  52.   
  53.     /////////////////////////////  
  54.     // 3. add your codes below...  
  55.   
  56.     // add a label shows "Hello World"  
  57.     // create and initialize a label  
  58.       
  59.     CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);  
  60.       
  61.     // position the label on the center of the screen  
  62.     pLabel->setPosition(ccp(origin.x + visibleSize.width/2,  
  63.                             origin.y + visibleSize.height - pLabel->getContentSize().height));  
  64.   
  65.     // add the label as a child to this layer  
  66.     this->addChild(pLabel, 1);  
  67.   
  68.     // add "HelloWorld" splash screen"  
  69.     CCSprite* pSprite = CCSprite::create("HelloWorld.png");  
  70.   
  71.     // position the sprite on the center of the screen  
  72.     pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));  
  73.   
  74.     // add the sprite as a child to this layer  
  75.     this->addChild(pSprite, 0);  
  76.       
  77.     return true;  
  78. }  
  79.   
  80.   
  81. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  82. {  
  83. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)  
  84.     CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");  
  85. #else  
  86.     CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(0.4f,Scene1::scene()));//核心的修改  
  87. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  88.     exit(0);  
  89. #endif  
  90. #endif  
  91. }  


这里,切换场景的特效,可以通过查询cocos2dx的API进行了解,常用的特效如下所示:

[cpp] view plain copy
 
  1. //慢慢淡化到另一场景  
  2. TransitionCrossFade::create(时间,目标场景);   
  3. //本场景变暗消失后另一场景慢慢出现  
  4. TransitionFade::create(时间,目标场景);   
  5. //本场景右上角到左下角方块消失到另一场景  
  6. TransitionFadeBL::create(时间,目标场景);   
  7. //本场景从上到下横条消失到另一场景  
  8. TransitionFadeDown::create(时间,目标场景);   
  9. //本场景左下角到右上角方块消失到另一场景  
  10. TransitionFadeTR::create(时间,目标场景);   
  11. //本场景从下到上横条消失到另一场景  
  12. TransitionFadeUp::create(时间,目标场景);   
  13. //本场景翻转消失到另一场景(斜上方)  
  14. TransitionFlipAngular::create(时间,目标场景,样式 );  
  15. //本场景翻转消失到另一场景(X轴)  
  16. TransitionFlipX::create(时间,目标场景,样式);  
  17. //本场景翻转消失到另一场景(Y轴)  
  18. TransitionFlipY::create(时间,目标场景);   
  19. //本场景跳动消失后另一场景跳动出现  
  20. TransitionJumpZoom::create(时间,目标场景);   
  21. //另一场景由整体从下面出现  
  22. TransitionMoveInB::create(时间,目标场景);   
  23. //另一场景由整体从左面出现  
  24. TransitionMoveInL::create(时间,目标场景);   
  25. //另一场景由整体从上面出现  
  26. TransitionMoveInT::create(时间,目标场景);   
  27. //另一场景由整体从右面出现  
  28. TransitionMoveInR::create(时间,目标场景);   
  29. //翻页切换,bool为true是向前翻。  
  30. TransitionPageTurn::create(时间,目标场景,bool);   
  31. //本场景从左到右消失同时另一场景出现  
  32. TransitionProgressHorizontal::create(时间,目标场景);  
  33. //本场景从中间到四周消失同时另一场景出现  
  34. TransitionProgressInOut::create(时间,目标场景);   
  35. //本场景从四周到中间消失同时另一场景出现  
  36. TransitionProgressOutIn::create(时间,目标场景);   
  37. //本场景逆时针消失到另一场景  
  38. TransitionProgressRadialCCW::create(时间,目标场景);   
  39. //本场景顺时针消失到另一场景  
  40. TransitionProgressRadialCW::create(时间,目标场景);   
  41. //本场景从上到下消失同时另一场景出现  
  42. TransitionProgressVertical::create(时间,目标场景);   
  43. //本场景旋转消失后另一场景旋转出现  
  44. TransitionRotoZoom::create(时间,目标场景);   
  45. //本场景缩小切换到另一场景放大  
  46. TransitionShrinkGrow::create(时间,目标场景);   
  47. //本场景向上滑动到另一场景  
  48. TransitionSlideInB::create(时间,目标场景);   
  49. //本场景向右滑动到另一场景  
  50. TransitionSlideInL::create(时间,目标场景);   
  51. //本场景向左滑动到另一场景  
  52. TransitionSlideInR::create(时间,目标场景);   
  53. //本场景向下滑动到另一场景  
  54. TransitionSlideInT::create(时间,目标场景);   
  55. //本场景三矩形上下消失后另一场景三矩形上下出现  
  56. TransitionSplitCols::create(时间,目标场景);   
  57. //本场景三矩形左右消失后另一场景三矩形左右出现  
  58. TransitionSplitRows::create(时间,目标场景);   
  59. //本场景小方块消失到另一场景  
  60. TransitionTurnOffTiles::create(时间,目标场景);   
  61. //本场景翻转消失到另一场景(斜上方)  
  62. TransitionZoomFlipAngular::create(时间,目标场景,样式);   
  63. //本场景翻转消失到另一场景(X轴)  
  64. TransitionZoomFlipX::create(时间,目标场景,样式);   
  65. //本场景翻转消失到另一场景(Y轴)  
  66. TransitionZoomFlipY::create(时间,目标场景,样式);  


6、最后,修改程序开场所展示的场景就完事了,具体是在AppDelegate.cpp中,先引入我们创建的Scene1.h,同时将第29行的Helloworld改为Scene1,同时关闭第23行难看的提示信息。具体修改如下:

[cpp] view plain copy
 
  1. #include "AppDelegate.h"  
  2. #include "HelloWorldScene.h"  
  3. #include "Scene1.h"  
  4.   
  5. USING_NS_CC;  
  6.   
  7. AppDelegate::AppDelegate() {  
  8.   
  9. }  
  10.   
  11. AppDelegate::~AppDelegate()   
  12. {  
  13. }  
  14.   
  15. bool AppDelegate::applicationDidFinishLaunching() {  
  16.     // initialize director  
  17.     CCDirector* pDirector = CCDirector::sharedDirector();  
  18.     CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();  
  19.   
  20.     pDirector->setOpenGLView(pEGLView);  
  21.       
  22.     // turn on display FPS  
  23.     pDirector->setDisplayStats(false);  
  24.   
  25.     // set FPS. the default value is 1.0/60 if you don't call this  
  26.     pDirector->setAnimationInterval(1.0 / 60);  
  27.   
  28.     // create a scene. it's an autorelease object  
  29.     CCScene *pScene = Scene1::scene();//修改启动的场景为Scene1  
  30.   
  31.     // run  
  32.     pDirector->runWithScene(pScene);  
  33.   
  34.     return true;  
  35. }  
  36.   
  37. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too  
  38. void AppDelegate::applicationDidEnterBackground() {  
  39.     CCDirector::sharedDirector()->stopAnimation();  
  40.   
  41.     // if you use SimpleAudioEngine, it must be pause  
  42.     // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  
  43. }  
  44.   
  45. // this function will be called when the app is active again  
  46. void AppDelegate::applicationWillEnterForeground() {  
  47.     CCDirector::sharedDirector()->startAnimation();  
  48.   
  49.     // if you use SimpleAudioEngine, it must resume here  
  50.     // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  
  51. }  

 

原文地址:https://www.cnblogs.com/bainiu/p/7592218.html