cocos2d-x 2.1.4学习笔记之HelloWorld分析

下面截图是HelloWorld项目下的文件夹结构

cocos2d-x 2.1.4学习笔记之HelloWorld分析

cocos2d-x 2.1.4学习笔记之HelloWorld分析

这是用python命令生成的项目,在创建过程中默认生成多个平台的程序文件。

1.“resource”文件夹

该文件夹主要用于存放游戏中需要的图片、音频和配置等资源文件。为了方便管理,可以在其中创建子文件夹。在不同平台下,对于文件路径的定义是不一致的,但实际接口大同小异。Cocos2d-x为我们屏蔽了这些差异,其中“resource”文件夹可以默认为游戏运行时的目录。

2.proj.win32文件夹

“main.h”、“main.cpp” 用于放置游戏头文件,它们是平台相关的程序文件,为Windows专有。通常情况下,程序入口与资源文件管理在不同平台下是不同的,但是Cocos2d-x的模板已经基本为我们处理好了这些细节,不需要对它们进行修改。

3.Classes文件夹

该文件夹里面存放的是项目所有的.h和.cpp文件。自然也包含了“AppDelegate.h”和“AppDelegate.cpp”文件。这两个文件是Cocos2d-x游戏的通用入口文件,类似于一般Windows工程中主函数所在的文件。

打开“AppDelegate.cpp”,我们可以看到已经自动添加的代码,这个文件实现了AppDelegate类。AppDelegate控制着游戏的生命周期,除去构造函数和析构函数外,共有3个方法,下面逐个介绍。

 
AppDelegate.h
   1:  #ifndef  _APP_DELEGATE_H_
   2:  #define  _APP_DELEGATE_H_
   3:   
   4:  #include "cocos2d.h"
   5:   
   6:  /**
   7:  @brief    The cocos2d Application.
   8:  The reason for implement as private inheritance is to hide some interface call by CCDirector.
   9:  */
  10:  class  AppDelegate : private cocos2d::CCApplication {
  11:  public:
  12:      AppDelegate();
  13:      virtual ~AppDelegate();
  14:   
  15:      /**
  16:      @brief    Implement CCDirector and CCScene init code here.
  17:      @return true    Initialize success, app continue.
  18:      @return false   Initialize failed, app terminate.
  19:      */
  20:      virtual bool applicationDidFinishLaunching();
  21:   
  22:      /**
  23:      @brief  The function be called when the application enter background
  24:      @param  the pointer of the application
  25:      */
  26:      virtual void applicationDidEnterBackground();
  27:   
  28:      /**
  29:      @brief  The function be called when the application enter foreground
  30:      @param  the pointer of the application
  31:      */
  32:      virtual void applicationWillEnterForeground();
  33:  };
  34:   
  35:  #endif // _APP_DELEGATE_H_
  36:   
 
 
AppDelegate.cpp
   1:  #include "AppDelegate.h"
   2:  #include "HelloWorldScene.h"
   3:   
   4:  USING_NS_CC;
   5:   
   6:  AppDelegate::AppDelegate() {
   7:  }
   8:   
   9:  AppDelegate::~AppDelegate() {
  10:  }
  11:   
  12:  bool AppDelegate::applicationDidFinishLaunching() {
  13:      //初始化游戏引擎控制器CCDirector,以便启动游戏引擎;
  14:      CCDirector* pDirector = CCDirector::sharedDirector();
  15:      CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();  16:   
  17:      pDirector->setOpenGLView(pEGLView);
  18:   
  19:      //启用FPS显示
  20:      pDirector->setDisplayStats(true);  21:   
  22:      // 设置绘制间隔(FPS); the default value is 1.0/60 if you don't call this
  24:      pDirector->setAnimationInterval(1.0 / 60);
  25:   
  26:      // create a scene. it's an autorelease object
  27:      CCScene *pScene = HelloWorld::scene();
  28:      // run
  29:      pDirector->runWithScene(pScene);
  30:      return true;
  31:  }
  32:   
  33:  // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
  34:  //当应用程序将要进入后台时,会调用这个方法。
  35:  //具体来说,当用户把程序切换到后台,或手机接到电话或短信后程序被系统切换到后台时,会调用这个方法。
  36:  //此时,应该暂停游戏中正在播放的音乐或音效。
  37:  //动作激烈的游戏通常也应该在此时进行暂停操作,以便玩家暂时离开游戏时不会遭受重大损失。
  38:  void AppDelegate::applicationDidEnterBackground() {
  39:      CCDirector::sharedDirector()->stopAnimation();
  40:      // if you use SimpleAudioEngine, it must be pause
  41:      // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
  42:  }
  43:   
  44:  // this function will be called when the app is active again
  45:  //该方法与applicationDidEnterBackground()成对出现,在应用程序回到前台时被调用。相对地,我们通常在这里继续播放刚才暂停的音乐,显示游戏暂停菜单等。
  46:  void AppDelegate::applicationWillEnterForeground() {
  47:      CCDirector::sharedDirector()->startAnimation();
  48:      // if you use SimpleAudioEngine, it must resume here
  49:      // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
  50:  }
  51:   
  52:  //FPS即每秒帧速率,也就是屏幕每秒重绘的次数。启用了FPS显示后,当前FPS会在游戏的左下角显示。通常在游戏开发阶段,我们会启用FPS显示,这样就可以方便地确定游戏运行是否流畅。
  54:  //绘制间隔指的是两次绘制的时间间隔,因此绘制间隔的倒数就是FPS上限。
  55:  //对于移动设备来说,我们通常都会将FPS限制在一个适当的范围内。
  56:  //过低的每秒重绘次数会使动画显示出卡顿的现象,而提高每秒重绘次数会导致设备运算量大幅增加,造成更高的能耗。
  57:  //人眼的刷新频率约为60次每秒,因此把FPS限定在60是一个较为合理的设置,Cocos2d-x就把绘制间隔设置为1/60秒。
  58:  //至此,我们已经完成了引擎的初始化,接下来我们将启动引擎;

HelloWorldScene.h

   1:  #ifndef __HELLOWORLD_SCENE_H__
   2:  #define __HELLOWORLD_SCENE_H__
   3:   
   4:  #include "cocos2d.h"
   5:   
   6:  class HelloWorld : public cocos2d::CCLayer {
   7:  public:
   8:      // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
   9:      virtual bool init();
  10:   
  11:      // there's no 'id' in cpp, so we recommend returning the class instance pointer
  12:      //在Cocos2d中,在层下设置一个创建场景的静态函数是一个常见的技巧
  13:      static cocos2d::CCScene* scene();
  14:   
  15:      // a selector callback
  16:      void menuCloseCallback(CCObject* pSender);
  17:   
  18:      // implement the "static node()" method manually
  19:      CREATE_FUNC(HelloWorld);
  20:  };
  21:   
  22:  #endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

   1:  #include "HelloWorldScene.h"
   2:   
   3:  USING_NS_CC;
   4:   
   5:  CCScene* HelloWorld::scene() {
   6:      // 'scene' is an autorelease object
   7:      CCScene *scene = CCScene::create();
   8:      // 'layer' is an autorelease object
   9:      HelloWorld *layer = HelloWorld::create();
  10:      // add layer as a child to scene
  11:      scene->addChild(layer);
  12:      // return the scene
  13:      return scene;
  14:  }
  15:   
  16:  // on "init" you need to initialize your instance
  17:  //为什么我们要在一个实例方法中初始化类,而不在构造函数中初始化呢?在C++中,一般习惯在构造函数中初始化类,然而由于Cocos2d-x的来源特殊,所以才没有采用C++的编程风格
  18:  //对父类进行初始化
  19:  bool HelloWorld::init() {
  20:      //////////////////////////////
  21:      // 1. super init first
  22:      if ( !CCLayer::init() ) {
  23:          return false;
  24:      }
  25:   
  26:      CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
  27:      CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
  28:   
  29:      /////////////////////////////
  30:      // 2. add a menu item with "X" image, which is clicked to quit the program
  31:      //    you may modify it.
  32:   
  33:      // add a "close" icon to exit the progress. it's an autorelease object
  34:      CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  35:                                        "CloseNormal.png",
  36:                                        "CloseSelected.png",
  37:                                        this,
  38:                                        menu_selector(HelloWorld::menuCloseCallback));
  39:   
  40:      pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
  41:                                  origin.y + pCloseItem->getContentSize().height/2));
  42:   
  43:      // create menu, it's an autorelease object
  44:      CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  45:      pMenu->setPosition(CCPointZero);
  46:      this->addChild(pMenu, 1);
  47:      //addChild(CCNode* child,int zOrder),参数zOrder指的是child的z轴顺序,也就是显示的先后顺序,其值越大,表示显示的位置就越靠前
  48:   
  49:      ///////////////////////////
  50:       //3. add your codes below...
  51:       //add a label shows "Hello World"
  52:       //create and initialize a label
  53:     CCLabelTTF* plabel = CCLabelTTF::create("hello world", "Arial", 24);
  54:      //position the label on the center of the screen
  55:     plabel->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height - plabel->getContentSize().height));
  56:      //add the label as a child to this layer
  57:     this->addChild(plabel, 1);
  58:   
  59:      //add "helloworld" splash screen"
  60:     CCSprite* psprite = CCSprite::create("helloworld.png");
  61:      //position the sprite on the center of the screen
  62:     psprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
  63:      //add the sprite as a child to this layer
  64:     this->addChild(psprite, 0);    
  65:      return true;
  66:  }
  67:   
  68:   
  69:  void HelloWorld::menuCloseCallback(CCObject* pSender) {
  70:      CCDirector::sharedDirector()->end();
  71:      #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  72:           exit(0);
  73:      #endif
  74:  }