经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
cocos2d-x学习之路(二)——分析AppDelegate和HelloWorldScene文件
来源:cnblogs  作者:VisualGMQ  时间:2018/9/25 19:27:50  对本文有异议

这里我们来看一下cocos自动给我们生成的工程里有些什么东西,并且分析一下这些代码的用途,来为我们以后编写cocos程序铺下基础。

这里我建议看我这份随笔的看官先看看cocos官网的快速入门手册,不然可能会比较迷糊(因为待会要分析一些代码,如果以前没见过的话会比较昏)。传送门在这里

其中一些基本不需要程序员干涉的代码我可能会不予分析。你也可以查看官方API手册。传送门在这里

下面的代码分析,如果是非常有用的东西我会在分析中用蓝色标出。


 首先我们进入相关的系统(你的如果是mac就打开proj.ios_mac文件夹下的工程,是windows就打开proj.win32文件夹下的工程,以此类推)的工程。我这里是mac,运行一下cocos给我们生成的代码,结果如下:

既然是自己的demo嘛,当然要给自己打个广告啦(那个居中的图标显然是cocos的logo)

这个界面里面有一些我们可以直接看出来的东西:

这里所有的元素都是我根据以往用过的引擎猜测的,实际上我们还是要看一下代码。不过我们目前知道大概有这么些东西了,待会可以针对着看一下。

然后我们可以看看这些资源在哪里,我通过XCODE可以直接看到: 

这里logo是HelloWorld.png,右下角的开关机图标是CloseNormal.png,而那个CloseSelected.png是按下按钮的图片。


 

分析AppDelegate

好的,到我们的分析阶段了。AppDelegate分为头文件和实现文件。我们当然是先看头文件啦。这个文件在Classes文件夹下。

  1. 1 #ifndef _APP_DELEGATE_H_
  2. 2 #define _APP_DELEGATE_H_
  3. 3
  4. 4 #include "cocos2d.h"
  5. 5
  6. 6 /**
  7. 7 @brief The cocos2d Application.
  8. 8
  9. 9 Private inheritance here hides part of interface from Director.
  10. 10 */
  11. 11 class AppDelegate : private cocos2d::Application
  12. 12 {
  13. 13 public:
  14. 14 AppDelegate();  //构造函数
  15. 15 virtual ~AppDelegate();  //析构函数
  16. 16
  17. 17 virtual void initGLContextAttrs();  //这个暂时不知道是干什么的
  18. 18
  19. 19 /**
  20. 20 @brief Implement Director and Scene init code here.
  21. 21 @return true Initialize success, app continue.
  22. 22 @return false Initialize failed, app terminate.
  23. 23 */
  24. 24 virtual bool applicationDidFinishLaunching();
  25. 25
  26. 26 /**
  27. 27 @brief Called when the application moves to the background
  28. 28 @param the pointer of the application
  29. 29 */
  30. 30 virtual void applicationDidEnterBackground();
  31. 31
  32. 32 /**
  33. 33 @brief Called when the application reenters the foreground
  34. 34 @param the pointer of the application
  35. 35 */
  36. 36 virtual void applicationWillEnterForeground();
  37. 37 };
  38. 38
  39. 39 #endif // _APP_DELEGATE_H_

第4行包含了cocos2d的头文件。

第11行定义了AppDelegate类,继承自cocos2d的Application类。

这里堆AppDelegate类的几个虚函数在注释上都有一定的说明了:

第24行的applicationDidFinishLaunching()是在程序初始化的时候自动调用的函数。在这里面我们可以初始化导演(Director)和场景(Scence)。如果程序初始化成果会返回True,否则返回False。

第30行的applicationDidEnterBackground()是在程序失去焦点的时候调用。这里面一般是加入用来停止程序的代码。

第36行的applicationWillEnterForeground()是在程序获得焦点的时候调用,可以在里面加入继续游戏的代码。

 

接下来看看实现文件:

  1. 1 #include "AppDelegate.h"
  2. 2 #include "HelloWorldScene.h"
  3. 3
  4. 4 // #define USE_AUDIO_ENGINE 1
  5. 5 // #define USE_SIMPLE_AUDIO_ENGINE 1
  6. 6
  7. 7 #if USE_AUDIO_ENGINE && USE_SIMPLE_AUDIO_ENGINE
  8. 8 #error "Don't use AudioEngine and SimpleAudioEngine at the same time. Please just select one in your game!"
  9. 9 #endif
  10. 10
  11. 11 #if USE_AUDIO_ENGINE
  12. 12 #include "audio/include/AudioEngine.h"
  13. 13 using namespace cocos2d::experimental;
  14. 14 #elif USE_SIMPLE_AUDIO_ENGINE
  15. 15 #include "audio/include/SimpleAudioEngine.h"
  16. 16 using namespace CocosDenshion;
  17. 17 #endif
  18. 18
  19. 19 USING_NS_CC;
  20. 20
  21. 21 static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320);
  22. 22 static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320);
  23. 23 static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768);
  24. 24 static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536);
  25. 25
  26. 26 AppDelegate::AppDelegate()
  27. 27 {
  28. 28 }
  29. 29
  30. 30 AppDelegate::~AppDelegate()
  31. 31 {
  32. 32 #if USE_AUDIO_ENGINE
  33. 33 AudioEngine::end();
  34. 34 #elif USE_SIMPLE_AUDIO_ENGINE
  35. 35 SimpleAudioEngine::end();
  36. 36 #endif
  37. 37 }
  38. 38
  39. 39 // if you want a different context, modify the value of glContextAttrs
  40. 40 // it will affect all platforms
  41. 41 void AppDelegate::initGLContextAttrs()    //这个真心没看懂是个啥
  42. 42 {
  43. 43 // set OpenGL context attributes: red,green,blue,alpha,depth,stencil,multisamplesCount
  44. 44 GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8, 0};
  45. 45
  46. 46 GLView::setGLContextAttrs(glContextAttrs);
  47. 47 }
  48. 48
  49. 49 // if you want to use the package manager to install more packages,
  50. 50 // don't modify or remove this function
  51. 51 static int register_all_packages()
  52. 52 {
  53. 53 return 0; //flag for packages manager
  54. 54 }
  55. 55
  56. 56 bool AppDelegate::applicationDidFinishLaunching() {
  57. 57 // initialize director
  58. 58 auto director = Director::getInstance();
  59. 59 auto glview = director->getOpenGLView();
  60. 60 if(!glview) {
  61. 61 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
  62. 62 glview = GLViewImpl::createWithRect("TestGame", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
  63. 63 #else
  64. 64 glview = GLViewImpl::create("TestGame");
  65. 65 #endif
  66. 66 director->setOpenGLView(glview);
  67. 67 }
  68. 68
  69. 69 // turn on display FPS
  70. 70 director->setDisplayStats(true);
  71. 71
  72. 72 // set FPS. the default value is 1.0/60 if you don't call this
  73. 73 director->setAnimationInterval(1.0f / 60);
  74. 74
  75. 75 // Set the design resolution
  76. 76 glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);  //不知道是干啥的
  77. 77 auto frameSize = glview->getFrameSize();
  78. 78 // if the frame's height is larger than the height of medium size.
  79. 79 if (frameSize.height > mediumResolutionSize.height)
  80. 80 {
  81. 81 director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
  82. 82 }
  83. 83 // if the frame's height is larger than the height of small size.
  84. 84 else if (frameSize.height > smallResolutionSize.height)
  85. 85 {
  86. 86 director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
  87. 87 }
  88. 88 // if the frame's height is smaller than the height of medium size.
  89. 89 else
  90. 90 {
  91. 91 director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
  92. 92 }
  93. 93
  94. 94 register_all_packages();
  95. 95
  96. 96 // create a scene. it's an autorelease object
  97. 97 auto scene = HelloWorld::createScene();
  98. 98
  99. 99 // run
  100. 100 director->runWithScene(scene);
  101. 101
  102. 102 return true;
  103. 103 }
  104. 104
  105. 105 // This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
  106. 106 void AppDelegate::applicationDidEnterBackground() {
  107. 107 Director::getInstance()->stopAnimation();
  108. 108
  109. 109 #if USE_AUDIO_ENGINE
  110. 110 AudioEngine::pauseAll();
  111. 111 #elif USE_SIMPLE_AUDIO_ENGINE
  112. 112 SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
  113. 113 SimpleAudioEngine::getInstance()->pauseAllEffects();
  114. 114 #endif
  115. 115 }
  116. 116
  117. 117 // this function will be called when the app is active again
  118. 118 void AppDelegate::applicationWillEnterForeground() {
  119. 119 Director::getInstance()->startAnimation();
  120. 120
  121. 121 #if USE_AUDIO_ENGINE
  122. 122 AudioEngine::resumeAll();
  123. 123 #elif USE_SIMPLE_AUDIO_ENGINE
  124. 124 SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
  125. 125 SimpleAudioEngine::getInstance()->resumeAllEffects();
  126. 126 #endif
  127. 127 }

 第18行以上的乱七八糟的东西我们就先不看了,无非就是包含头文件啊,使用命名空间什么的。这些暂时不管。

第19行是一个宏,可以进入到里面看看(这个宏后面用到的还比较多):

  1. #define USING_NS_CC using namespace cocos2d

 可以看到就是使用cocos2d的命名空间。

  • 第21~24行定义了四种大小(从cocos2d::Size可以看出是大小的定义),按照变量名字分别是设计时大小,最小化大小,通常化大小和最大化大小。这里我还是要重复说一下:现在这些只是我们的猜测,具体的还是要看到相关代码才行。

那么我们可以想,我是不是更改一下这些玩意就可以改变窗口大小了呢?嗯……可以尝试一下,就先从designResolutionSize下手吧。

我这里将designResolutionSize参数改成1024,480,果然窗口的大小改变了: 

不过改变了其他三个尺寸之后窗口没什么变化。暂时先不管吧,反正现在知道designResolutionSize变量存储的是当前窗口的大小就行了

 

  • 30~37行是析构函数的实现。可以看出是释放了AudioEngine和SimpleAudioEngine两个模块。
  • 51行的函数用于管理包。当你想要使用包管理器(Package Manager)来安装多个包的时候,就不要修改或者删除这里的代码。(你问我包是啥?对不起我也不晓得。你说那你怎么知道这个是包管理器函数?看注释啊??)
  • 56~103是个大函数,为程序初始化的时候调用的函数。让我们来慢慢分析:
    • 58行初始化了导演。按照官方快速入门手册的说法,导演(Direct)是一个单例对象(关于单例请见单例模式),主要用于切换场景啊,维护层啊什么的,就和我们生活中的导演差不多。
    • 59~67行初始化了视图。并且对视图创建失败做了一定的检测。最后将这个视图指定为Direct的视图。
    • 70行打开了FPS显示。这里你可以把参数改为false,运行程序你会发下左下角的调试信息消失了。这个比较重要,因为我们的游戏最后发布的时候是不需要显示FPS信息的。
    • 73行设置了这个程序的帧率,即FPS。从参数可以看出是60帧每秒(FPS=60)。这个到时候在制作自己的游戏的时候可以根据实际情况改一改。
    • 79~92行分别对窗口大小大于smallResolutionSize ,mediumResolutionSize ,largeResolutionSize时做出行动。这里的setContentScaleFactor函数是“改变Surface里像素的大小”。
    • 96行创建了一个场景(Scene)。根据官方手册知道场景是用来容纳物体(比如Sprite啊什么的)的。
    • 100行说明在程序开始的时候运行这个场景
  • 接下来的106~115行是用来指定窗口获得焦点时的相关行动
  • 118~127则是窗口失去焦点的相关行动

 

这里关于APPDelegate文件就分析完了。我们看出来好像这个文件里面没有什么和我们一开始看到的界面元素有关系。好像都是一些对游戏初始化的工作。所以以后我们不会大幅度更改这里面的代码。


分析HelloWorldScene文件

 那么首先还是来看看其头文件:

  1. 1 #ifndef __HELLOWORLD_SCENE_H__
  2. 2 #define __HELLOWORLD_SCENE_H__
  3. 3
  4. 4 #include "cocos2d.h"
  5. 5
  6. 6 class HelloWorld : public cocos2d::Scene
  7. 7 {
  8. 8 public:
  9. 9 static cocos2d::Scene* createScene();
  10. 10
  11. 11 virtual bool init();
  12. 12
  13. 13 // a selector callback
  14. 14 void menuCloseCallback(cocos2d::Ref* pSender);
  15. 15
  16. 16 // implement the "static create()" method manually
  17. 17 CREATE_FUNC(HelloWorld);
  18. 18 };
  19. 19
  20. 20 #endif // __HELLOWORLD_SCENE_H__

 这个头文件挺简单的,定义了一个HelloWorld类继承自cocos2d::Scene。

  • 第14行的函数定义了一个菜单关闭的回调函数(你说,我咋没在界面上看到有菜单啊?别急,让我们来一步一步看看这个菜单是从哪里来的)
  • 第17行使用了CREATE_FUNC宏,用于创建层。我们可以跳转到其定义看一下:
    1. #define CREATE_FUNC(__TYPE__) static __TYPE__* create() { __TYPE__ *pRet = new(std::nothrow) __TYPE__(); if (pRet && pRet->init()) { pRet->autorelease(); return pRet; } else { delete pRet; pRet = nullptr; return nullptr; } }

     显然,这个宏其实是定义了一个static  __TYPE__*  create()函数。

 

接下来看看实现文件:

  1. 1 #include "HelloWorldScene.h"
  2. 2 #include "SimpleAudioEngine.h"
  3. 3
  4. 4 USING_NS_CC;
  5. 5
  6. 6 Scene* HelloWorld::createScene()
  7. 7 {
  8. 8 return HelloWorld::create();
  9. 9 }
  10. 10
  11. 11 // Print useful error message instead of segfaulting when files are not there.
  12. 12 static void problemLoading(const char* filename)
  13. 13 {
  14. 14 printf("Error while loading: %s\n", filename);
  15. 15 printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
  16. 16 }
  17. 17
  18. 18 // on "init" you need to initialize your instance
  19. 19 bool HelloWorld::init()
  20. 20 {
  21. 21 //////////////////////////////
  22. 22 // 1. super init first
  23. 23 if ( !Scene::init() )
  24. 24 {
  25. 25 return false;
  26. 26 }
  27. 27
  28. 28 auto visibleSize = Director::getInstance()->getVisibleSize();
  29. 29 Vec2 origin = Director::getInstance()->getVisibleOrigin();
  30. 30
  31. 31 /////////////////////////////
  32. 32 // 2. add a menu item with "X" image, which is clicked to quit the program
  33. 33 // you may modify it.
  34. 34
  35. 35 // add a "close" icon to exit the progress. it's an autorelease object
  36. 36 auto closeItem = MenuItemImage::create(
  37. 37 "CloseNormal.png",
  38. 38 "CloseSelected.png",
  39. 39 CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
  40. 40
  41. 41 if (closeItem == nullptr ||
  42. 42 closeItem->getContentSize().width <= 0 ||
  43. 43 closeItem->getContentSize().height <= 0)
  44. 44 {
  45. 45 problemLoading("'CloseNormal.png' and 'CloseSelected.png'");
  46. 46 }
  47. 47 else
  48. 48 {
  49. 49 float x = origin.x + visibleSize.width - closeItem->getContentSize().width/2;
  50. 50 float y = origin.y + closeItem->getContentSize().height/2;
  51. 51 closeItem->setPosition(Vec2(x,y));
  52. 52 }
  53. 53
  54. 54 // create menu, it's an autorelease object
  55. 55 auto menu = Menu::create(closeItem, NULL);
  56. 56 menu->setPosition(Vec2::ZERO);
  57. 57 this->addChild(menu, 1);
  58. 58
  59. 59 /////////////////////////////
  60. 60 // 3. add your codes below...
  61. 61
  62. 62 // add a label shows "Hello World"
  63. 63 // create and initialize a label
  64. 64
  65. 65 auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
  66. 66 if (label == nullptr)
  67. 67 {
  68. 68 problemLoading("'fonts/Marker Felt.ttf'");
  69. 69 }
  70. 70 else
  71. 71 {
  72. 72 // position the label on the center of the screen
  73. 73 label->setPosition(Vec2(origin.x + visibleSize.width/2,
  74. 74 origin.y + visibleSize.height - label->getContentSize().height));
  75. 75
  76. 76 // add the label as a child to this layer
  77. 77 this->addChild(label, 1);
  78. 78 }
  79. 79
  80. 80 // add "HelloWorld" splash screen"
  81. 81 auto sprite = Sprite::create("HelloWorld.png");
  82. 82 if (sprite == nullptr)
  83. 83 {
  84. 84 problemLoading("'land.png'");
  85. 85 }
  86. 86 else
  87. 87 {
  88. 88 // position the sprite on the center of the screen
  89. 89 sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
  90. 90
  91. 91 // add the sprite as a child to this layer
  92. 92 this->addChild(sprite, 0);
  93. 93 }
  94. 94 return true;
  95. 95 }
  96. 96
  97. 97
  98. 98 void HelloWorld::menuCloseCallback(Ref* pSender)
  99. 99 {
  100. 100 //Close the cocos2d-x game scene and quit the application
  101. 101 Director::getInstance()->end();
  102. 102
  103. 103 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  104. 104 exit(0);
  105. 105 #endif
  106. 106
  107. 107 }
  • 第六行的createScene()函数调用了HelloWorld类的create方法创建了一个层。你说诶不对啊,上面那个HelloWorld类的定义里面没有create方法呀。这个时候你可以回过头再去看看CREATE_FUNC宏的代码。其实CREATE_FUNC函数就是create函数啦。这个函数被APPDelegate类调用过(见APPDelegate的实现文件97行)
  • 12~16行定义的函数用于输出错误信息
  • 19~95行定义了HelloWorld类的init函数。这个函数是在CREATE_FUNC里面被调用的。我们来看看这个函数里面到底有些啥
    • 23~26行为第一步——创建Scene
    • 36~57创建菜单(原来菜单在这里,赶紧看看那这个菜单是什么)
      • 首先36行通过MenuItemImage的create方法创建一个菜单组件。注意到这里面的参数!这个参数正是我们前面在界面上看到的开关机图像!原来那个东西不是按钮,是一个菜单。
      • 41~52行判断是否创建菜单项成功,并且调整菜单项位置为右下角
      • 55行通过菜单组件创建一个菜单
      • 56行设置菜单位置
      • 57行将菜单放到层的树里面(就是放到层中)
    • 65~95行,注释上写着“加入你自己的代码”。我们来看看cocos给我们加了什么代码
      • 65行创建了一个label,而且是使用TTF字体创建的。用于显示HelloWorld(果然HelloWorld是个label)
      • 66~78行判断label是否创建成功,并且设置了label的位置,加入了层。
      • 81行创建了一个Sprite,也就是精灵。用于绘制那个logo。
      • 82~93行同样是判断精灵是否创建成功,然后修改位置,加入层。 
    • 最后98~107定义了菜单的回调函数。这里如果点击了就会推出程序。然后这里有一个比较重要的东西:CC_TARGET_PLATFORM常量。这个常量存储着这个程序运行的操作系统。这里判断是否为IOS操作系统。我们可以使用这个常量来获得当前操作系统。

那么知道了这些之后,你可以更改一下代码来产生自己的界面。我这里改变了label的显示,并且改变了logo(把你要替换logo的图片放在Resource下,而且最好是png。我一开始使用bmp不能读取):

 

 

至此,我们的demo就分析完成了。


函数调用的过程

可能看完上面的分析有些人会有些头昏:“这说的都是啥和啥啊我现在脑子里一片混乱”。没关系,下面我们使用函数之间的调用图来说明一下这四个文件到底干了什么: 

 

这下清楚许多了吧。


 

总结

现在我们来总结一些常用的代码:

  • designResolutionSize变量用于设定窗口大小
  • director->setDisplayStats(true);用于设置是否显示FPS
  • director->setAnimationInterval(1.0f / 60);用于设置FPS

其他的代码,有的涉及到菜单和label的,我们放到以后的专题去说。

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号