零基础学cocos2dx 3.0(顺带学C++)第一篇 切换场景

来源:互联网 发布:java 判断奇数偶数 编辑:程序博客网 时间:2024/05/21 14:48

不会去重复一些概念性的东西,具体的内容,我在代码中已经备注出来。先看目录,这个创建完项目目录的Classes文件夹下有4个文件,我把helloworld那两个文件直接改了名字,这里我叫MainScene 。切记,在改为名字之后需要到appDelegate.pp里面把对应的helloworld改过来。

废话不多说,直接上代码

1,appDelegate.h

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #ifndef  _APP_DELEGATE_H_  
  2. #define  _APP_DELEGATE_H_  
  3.   
  4. #include "cocos2d.h"  
  5.   
  6. /** 
  7.  * cocos2d 应用入口类 
  8.  * 官方解释这里使用私有继承的原因是隐藏了一些导演类的接口 
  9.  * C++ 不是很懂,后期再好好研究下 
  10.  */  
  11. class  AppDelegate : private cocos2d::Application  
  12. {  
  13. public:  
  14.     //构造函数,构造一个AppDelegate  
  15.     AppDelegate();  
  16.     //析构函数,用于回收垃圾,释放空间,这里是虚函数,建议包括本人在内的new C++er好好补习下C++基础  
  17.     //这里找到资料为:C++通过虚函数实现多态."无论发送消息的对象属于什么类,它们均发送具有同一形式的消息,对消息的处理方式可能随接手消息的对象而变"的处理方式被称为多态性。  
  18.     virtual ~AppDelegate();  
  19.   
  20.     /** 
  21.      * 主程序的入口方法,在这里对导演和场景的初始化 
  22.      @return true   初始化 success, 应用 continue. 
  23.      @return false   初始化 failed, 应用 terminate. 
  24.      */  
  25.     virtual bool applicationDidFinishLaunching();  
  26.   
  27.     /** 
  28.      * 程序转入背景运行函数 
  29.      */  
  30.     virtual void applicationDidEnterBackground();  
  31.   
  32.     /** 
  33.      * 程序恢复运行函数 
  34.      */  
  35.     virtual void applicationWillEnterForeground();  
  36. };  
  37.   
  38. #endif // _APP_DELEGATE_H_  

2,appDelegate.cpp


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include "AppDelegate.h"  
  2. #include "MainScene.h" //这里原本是HelloWorldScene,我换了名字  
  3. //使用cocos2dx的命名空间,如果不使用的话,貌似有些cocos的字段显示无法识别  
  4. USING_NS_CC;  
  5. //构建入口类  
  6. AppDelegate::AppDelegate() {  
  7.   
  8. }  
  9. //析构函数,用于垃圾回收  
  10. AppDelegate::~AppDelegate()   
  11. {  
  12. }  
  13. //启动程序入口  
  14. bool AppDelegate::applicationDidFinishLaunching() {  
  15.     // 初始化导演类  
  16.     auto director = Director::getInstance();  
  17.     //通过导演类获取一个OpenGL坐标系  
  18.     auto glview = director->getOpenGLView();  
  19.     //如果没有获取到,创建一个坐标系界面,命名为Cocos2dxSceneShow  
  20.     if(!glview) {  
  21.         glview = GLView::create("Cocos2dxSceneShow");  
  22.         director->setOpenGLView(glview);  
  23.     }  
  24.   
  25.     // 打开显示帧率  
  26.     director->setDisplayStats(true);  
  27.   
  28.     // 设置默认帧率为1.0/60,即每秒60帧  
  29.     director->setAnimationInterval(1.0 / 60);  
  30.   
  31.     // 创建一个允许被释放的场景MainScene  
  32.     auto scene = MainScene::scene();  
  33.   
  34.     // 入口,运行这个MainScene场景  
  35.     director->runWithScene(scene);  
  36.   
  37.     return true;  
  38. }  
  39.   
  40. // 暂停应用的时候调用。比如来了电话  
  41. void AppDelegate::applicationDidEnterBackground() {  
  42.     Director::getInstance()->stopAnimation();  
  43.   
  44.     // 如果用了音乐等 SimpleAudioEngine, 这里需要加上  
  45.     // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();  
  46. }  
  47.   
  48. // 重新唤醒游戏调用  
  49. void AppDelegate::applicationWillEnterForeground() {  
  50.     Director::getInstance()->startAnimation();  
  51.   
  52.     // 如果用了音乐等 这里需加上  
  53.     // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();  
  54. }  

3,MainScene.h(helloworld那个类)


[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #ifndef __MainScene_H__  
  2. #define __MainScene_H__  
  3.   
  4. #include "cocos2d.h"  
  5. //创建一个MainScene类继承自cocos2d的Layer类  
  6. class MainScene : public cocos2d::Layer  
  7. {  
  8. public:  
  9.     //初始化函数,我用来定义组件  
  10.     virtual bool init();  
  11.     //创建一个场景  
  12.     static cocos2d::Scene* scene();  
  13.     //转场第一个界面  
  14.     void onPushSceneOne(Ref* pSender);  
  15.     //转场第二个界面  
  16.     void onPushSceneTwo(Ref* pSender);  
  17.     //关闭方法  
  18.     void menuCloseCallback(Ref* pSender);  
  19.     //创建场景宏  
  20.     CREATE_FUNC(MainScene);  
  21. };  
  22. #endif // __HELLOWORLD_SCENE_H__  

4,MainScene.cpp

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //这个类开始在项目生成的时候是helloworld类,这里我改了一下  
  2. #include "MainScene.h"  
  3. //项目中调用了NewScene所以需要引用  
  4. #include "NewScene.h"  
  5. #include "GameScene.h"  
  6. USING_NS_CC;  
  7. //创建主场景  
  8. Scene* MainScene::scene()  
  9. {  
  10.     //使用场景类创建一个场景  
  11.     auto scene = Scene::create();  
  12.     //创建一个MainScene层  
  13.     MainScene *layer = MainScene::create();  
  14.     //把层添加到场景里面去  
  15.     scene->addChild(layer);  
  16.     // 返回一个场景  
  17.     return scene;  
  18. }  
  19.   
  20. // 初始化一个MainScene层  
  21. bool MainScene::init()  
  22. {  
  23.     //层的初始化是否成功  
  24.     if (!Layer::init())  
  25.     {  
  26.         return false;  
  27.     }  
  28.     //获取坐标系界面的屏幕可见区域  
  29.     Size visibleSize = Director::getInstance()->getVisibleSize();  
  30.     //原点位置  
  31.     Point origin = Director::getInstance()->getVisibleOrigin();  
  32.     //创建一个关闭按钮,参数分别为,正常状态显示的图片,选中状态的图片,操作调用的方法即调用MainScene的menuCloseCallback方法  
  33.     auto closeItem = MenuItemImage::create(  
  34.         "CloseNormal.png",  
  35.         "CloseSelected.png",  
  36.         CC_CALLBACK_1(MainScene::menuCloseCallback, this));  
  37.     //设置关闭按钮的位置为视图的右下方  
  38.     closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width / 2,  
  39.         origin.y + closeItem->getContentSize().height / 2));  
  40.     //创建菜单,请注意,最后一个参数一定是NULL  
  41.     auto menu = Menu::create(closeItem, NULL);  
  42.     //初始化菜单的位置为原点位置,这个我也需要找个时间理解下  
  43.     menu->setPosition(Point::ZERO);  
  44.     //把菜单添加到层中  
  45.     this->addChild(menu, 1);  
  46.     //在视图的正中间竖向位置添加2个图片按钮  
  47.     auto item1 = MenuItemImage::create("btn-play-normal.png""btn-play-selected.png", CC_CALLBACK_1(MainScene::onPushSceneOne, this));  
  48.     auto item2 = MenuItemImage::create("btn-about-normal.png""btn-about-selected.png", CC_CALLBACK_1(MainScene::onPushSceneTwo, this));  
  49.     auto menu_oper = Menu::create(item1, item2, NULL);  
  50.     menu_oper->alignItemsVerticallyWithPadding(50);  
  51.   
  52.     this->addChild(menu_oper, 1);  
  53.   
  54.     return true;  
  55. }  
  56. /** 
  57.  * 进入NewScene的方法 
  58.  */  
  59. void MainScene::onPushSceneOne(Ref* pSender){  
  60.     auto scene = Scene::create();  
  61.     scene->addChild(SceneTestLayer::create());  
  62.     Director::getInstance()->replaceScene(TransitionFlipX::create(.5, scene));  
  63. }  
  64. /** 
  65.  *进入GameScene的方法 
  66.  */  
  67. void MainScene::onPushSceneTwo(Ref* pSender){  
  68.     auto scene = Scene::create();  
  69.     scene->addChild(GameSceneLayer::create());  
  70.     Director::getInstance()->replaceScene(TransitionFlipX::create(.5, scene));  
  71. }  
  72. /** 
  73.  * 关闭视窗的方法 
  74.  * 下面是官方定义好的一个宏,意思就是,当平台为wp8或者winRT会弹出框 
  75.  */  
  76. void MainScene::menuCloseCallback(Ref* pSender)  
  77. {  
  78. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)  
  79.     MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.""Alert");  
  80.     return;  
  81. #endif  
  82.     Director::getInstance()->end();  
  83.   
  84. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  85.     exit(0);  
  86. #endif  
  87. }  

5,NewScene.h

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #ifndef __NewScene_H__  
  2. #define __NewScene_H__  
  3.   
  4. #include "cocos2d.h"  
  5. //创建一个SceneTestLayer类继承自cocos2d的Layer类  
  6. class SceneTestLayer : public cocos2d::Layer  
  7. {  
  8. public:  
  9.     //构造函数  
  10.     SceneTestLayer();  
  11.     //初始化函数  
  12.     bool init();  
  13.     //返回主界面函数  
  14.     void backToMainScene(Ref* sender);  
  15.     //创建SceneTestLayer层宏  
  16.     CREATE_FUNC(SceneTestLayer)  
  17. };  
  18. #endif  

6,NewScene.cpp

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include "NewScene.h"  
  2. #include "MainScene.h"  
  3. USING_NS_CC;  
  4. //构造函数,这里没做什么  
  5. SceneTestLayer::SceneTestLayer(){}  
  6. /** 
  7.  * 初始化函数,用来初始化界面组件 
  8.  * 下面的代码很容易理解,我就不一一注解了 
  9.  */  
  10. bool SceneTestLayer::init()  
  11. {  
  12.     if (Layer::init())  
  13.     {  
  14.         auto textItem = MenuItemFont::create("This is NewSence");  
  15.         auto item = MenuItemFont::create("Back", CC_CALLBACK_1(SceneTestLayer::backToMainScene, this));  
  16.         auto menu = Menu::create(textItem,item, NULL);  
  17.         menu->alignItemsVertically();  
  18.         this->addChild(menu);  
  19.         return true;  
  20.     }  
  21.     return false;  
  22. }  
  23. /** 
  24.  * 返回主界面的函数 
  25.  */  
  26. void SceneTestLayer::backToMainScene(Ref* sender){  
  27.     auto scene = Scene::create();  
  28.     scene->addChild(MainScene::create());  
  29.     Director::getInstance()->replaceScene(TransitionFlipX::create(.5, scene));  
  30. }  

7,GameScene.h

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #ifndef __GameSceneLayer_H__  
  2. #define __GameSceneLayer_H__  
  3.   
  4. #include "cocos2d.h"  
  5. class GameSceneLayer : public cocos2d::Layer  
  6. {  
  7. public:  
  8.     GameSceneLayer();  
  9.     bool init();  
  10.     void backToMainScene(Ref* sender);  
  11.     CREATE_FUNC(GameSceneLayer)  
  12. };  
  13.   
  14. #endif  

8,GameScene.cpp

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include "GameScene.h"  
  2. #include "MainScene.h"  
  3. USING_NS_CC;  
  4. GameSceneLayer::GameSceneLayer(){}  
  5. bool GameSceneLayer::init()  
  6. {  
  7.     if (Layer::init())  
  8.     {  
  9.         auto testItem = MenuItemFont::create("This is GameScene");  
  10.         auto item = MenuItemFont::create("Back", CC_CALLBACK_1(GameSceneLayer::backToMainScene, this));  
  11.         auto menu = Menu::create(testItem, item, NULL);  
  12.         //竖向排列,间距50  
  13.         menu->alignItemsVerticallyWithPadding(50);  
  14.         this->addChild(menu);  
  15.         return true;  
  16.     }  
  17.     return false;  
  18. }  
  19. void GameSceneLayer::backToMainScene(Ref* sender){  
  20.     auto scene = Scene::create();  
  21.     scene->addChild(MainScene::create());  
  22.     Director::getInstance()->replaceScene(TransitionFlipX::create(.5, scene));  
  23. }  

OK,代码就这些,我在官方的demo(testcpp里面找了4张图片这里就不解释了)直接放到resource文件夹下面就行了

运行效果如下,简单的说就是3个层的切换

主界面如下:


点击play图片,切换到NewScene:



点击back可返回

点击MainScene的aboat图片,可进入GameScene界面


OK,就是这么简单,当然,这才刚开始,因为到本人写这篇博客为止,我看cocos2dx的资料时间也就2天,这个东西也是模拟官方demo弄的,非常的坑,不过刚开始练手还是可以的。



FROM: http://blog.csdn.net/helem_2013/article/details/36647333

0 0