Cocos2d-3.x版的HelloWorld工程分析 (二)

举报
鱼酱 发表于 2022/01/06 23:05:23 2022/01/06
4.2k+ 0 0
【摘要】 我们HelloWorld 从applicationDidFinishLaunching()后, 大部分人都会从这部分代码开始研究,如果想要研究main函数 如何调用applicationDidFinishLaunching()  传送门 http://blog.csdn.net/hiwoshixiaoyu/ar...

大笑大笑大笑大笑大笑大笑大笑大笑

我们HelloWorld 从applicationDidFinishLaunching()后,

大部分人都会从这部分代码开始研究,如果想要研究main函数 如何调用applicationDidFinishLaunching() 

传送门 http://blog.csdn.net/hiwoshixiaoyu/article/details/51472707



      #include "AppDelegate.h"
      #include <vector>
      #include <string>
      #include "HelloWorldScene.h"
      #include "AppMacros.h"
      //Uncomment the following line to use localize manager
      //#include "editor-support/cocostudio/LocalizationManager.h"
      //cocos2d的命名空间
      USING_NS_CC;
      //标准命名空间
      using namespace std;
      //构造函数
      AppDelegate::AppDelegate() {
      }
      //析构函数
      AppDelegate::~AppDelegate()
      {
      }
      //初始化OpenGl的上下文属性
      void AppDelegate::initGLContextAttrs()
      {
          GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
          GLView::setGLContextAttrs(glContextAttrs);
      }
      bool AppDelegate::applicationDidFinishLaunching() {
         // 获取导演实例
         auto director = Director::getInstance();
         //获取导演的GLView对象(通过一些函数,GLView可以操作的帧信息EGL视图)
     	//GLView是一个抽象类
     	//cocos2d-x提供GLViewImpl,继承它为默认渲染器上下文,你也可以有自己的GLViewImpl,通过继承重写
     	auto glview = director->getOpenGLView();
     	//判空
         if(!glview) {
             //创建窗口名
      		glview = GLViewImpl::create("Cpp Empty Test");
             //设置opengl的视图
      		director->setOpenGLView(glview);
          }
          director->setOpenGLView(glview);
         // 设置决定的分辨率
          glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
     	//获取EGL视图大小
      	Size frameSize = glview->getFrameSize();
     	//一个string容器
          vector<string> searchPath;
     	//在这个例子里,我们选择资源根据画面的高度
     	//如果资源不同于我们的分辨率大小,你需要设置一个contentScaleFactor
     	//我们使用资源的高度比的高度设计解决
     	//这样我们可以确保资源的高度 填充设计分辨率的高度
     	//如果画面的高度比设备的小,选择大的资源
     	if (frameSize.height > mediumResource.size.height)
      	{
     		//高度大于设备高度
     		//设置选择资源路径
              searchPath.push_back(largeResource.directory);
     		//设置比例(高分辨率的设备可能有一个更高的表面比屏幕尺寸大小)
              director->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));
      	}
     	//如果画面高度大于设置资源高度,选择设置资源
         else if (frameSize.height > smallResource.size.height)
          {
              searchPath.push_back(mediumResource.directory);
              director->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width));
          }
     	//如果画面高度小于设置资源高度,选择小的资源
     	else
          {
              searchPath.push_back(smallResource.directory);
              director->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));
          }
         //设置搜索目录
          FileUtils::getInstance()->setSearchPaths(searchPath);
         // 如果你想要加载json,用下面的方法
         /*
       cocostudio::ILocalizationManager * lm = cocostudio::JsonLocalizationManager::getInstance();
       lm->initLanguageData("your localize file name.lang.json");
       cocostudio::LocalizationHelper::setCurrentManager(lm, false);
       */
     	//如果你想要 载入二进制数据 .csb 用一下方法
         /*
       cocostudio::ILocalizationManager * lm = cocostudio::BinLocalizationManager::getInstance();
       lm->initLanguageData("your localize file name.lang.csb");
       cocostudio::LocalizationHelper::setCurrentManager(lm, true);
       */
         // 显示 FPS
          director->setDisplayStats(true);
         // 设置 FPS.默认值是 1.0/60 
          director->setAnimationInterval(1.0 / 60);
         // 创建一个自动释放的场景对象,通过一个静态方法
         auto scene = HelloWorld::scene();
         // 运行场景
          director->runWithScene(scene);
         return true;
      }
      //这个函数将被调用,当app是不活跃的。例如打来电话了,它就会被唤起
      void AppDelegate::applicationDidEnterBackground() {
          Director::getInstance()->stopAnimation();
         // 如果使用 SimpleAudioEngine,必须在这pause
         // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
      }
      //当app再次活跃起来的时候,这个函数会被调用
      void AppDelegate::applicationWillEnterForeground() {
          Director::getInstance()->startAnimation();
         // 如果使用SimpleAudioEngine, 你必须在这 resume 
         // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
      }
  
 



AppDelegate.cpp中的代码主要实现了游戏启动后执行的操作,游戏启动后的操作:

1、初始化导演类

2、创建的窗口

3、设置openGL 视图

4、设置动画的帧数显示和帧率

5、调用场景

6、运行场景(游戏真正的开始)



      #ifndef __HELLOWORLD_SCENE_H__
      #define __HELLOWORLD_SCENE_H__
      #include "cocos2d.h"
      class HelloWorld : public cocos2d::Layer
      {
      public:
     	//初始化层 
         virtual bool init() override;
     	//创建场景 
         static cocos2d::Scene* scene();
         // 响应消息 
         void menuCloseCallback(Ref* sender);
         // 
     	//用于创建:场景、菜单、层等东西 ,宏函数
         CREATE_FUNC(HelloWorld);
      };
      #endif // __HELLOWORLD_SCENE_H__
  
 


      #include "HelloWorldScene.h"
      #include "AppMacros.h"
      USING_NS_CC;
      Scene* HelloWorld::scene()
      {
     	//'scene'是一个自动释放的对象
         auto scene = Scene::create();
         // 'layer'是一个自动释放的对象
          HelloWorld *layer = HelloWorld::create();
         // 把 layer作为一个儿子加到 scene上
          scene->addChild(layer);
         // 返回 scene
         return scene;
      }
      // 在 "init" 你需要初始化你想要的实例
      bool HelloWorld::init()
      {
         //
         // 1. 父类 首先初始化
         if ( !Layer::init() )
          {
             return false;
          }
     	/************************************************************************/
     	/* FrameSize就是屏幕的实际分辨率,这是不变的,比如我用的盖世三的手机分辨率为1280x720,这就是盖世三的FrameSize。
       注意这个FrameSize很容易理解成屏幕的长宽多少,其实不是,分辨率和实际的长宽没有必然联系。
       比如:
       盖世3的尺寸(长宽)为4.8寸,分辨率为1280x720
       盖世4的尺寸(长宽)为4.99寸,分辨率为1920x1080
       所以在设计游戏时,只考虑屏幕的分辨率即可,不用考虑屏幕的实际长宽。 */
     	/************************************************************************/
         /*
       WinSize就是设计分辨率,相当于游戏设计的逻辑大小,可以这样理解,上面的FrameSize就是画框,这里的WinSize就是画布。
       VisibleSize就是画布显示在画框中的部分,注意:它的大小是用WinSize来表示的。
       VisibleOrigin就是VisibleSize在画框中的左下角坐标点,注意也是用WinSize来表示的。
      */
         auto visibleSize = Director::getInstance()->getVisibleSize();
         auto origin = Director::getInstance()->getVisibleOrigin();
         // 添加一个关闭按钮icon,设置回调函数,这是一个自动释放的对象
         auto closeItem = MenuItemImage::create(
                                             "CloseNormal.png",
                                             "CloseSelected.png",
                                             CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
          closeItem->setPosition(origin + Vec2(visibleSize) - Vec2(closeItem->getContentSize() / 2));
     	//常见一个菜单,它是一个自动释放的对象
         auto menu = Menu::create(closeItem, nullptr);
          menu->setPosition(Vec2::ZERO);
         this->addChild(menu, 1);
         //常见一个label标签,字体是arial.ttf
         auto label = Label::createWithTTF("Hello World", "fonts/arial.ttf", TITLE_FONT_SIZE);
         // label的位置设置到屏幕中心
          label->setPosition(origin.x + visibleSize.width/2,
                                  origin.y + visibleSize.height - label->getContentSize().height);
     	//添加标签到这个层上
         this->addChild(label, 1);
         // 添加HelloWorld图片
         auto sprite = Sprite::create("HelloWorld.png");
         //精灵的位置在屏幕中心
          sprite->setPosition(Vec2(visibleSize / 2) + origin);
         // 把精灵作为儿子加到层上
         this->addChild(sprite);
         return true;
      }
      //回调函数
      void HelloWorld::menuCloseCallback(Ref* sender)
      {
     	//Director结束执行,释放资源
          Director::getInstance()->end();
      #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
     	//IOS的退出
         exit(0);
      #endif
      }
  
 

HelloWorldScene.cpp中的代码中的Scene* HelloWorld::scene(),实现了创建场景的过程:

1、创建场景

2、创建层 

3 、将层加到场景上

4、返回场景

HelloWorldScene.cpp中的代码中的Scene* HelloWorld::init(),实现了初始化实例:

1、初始化父类的Layer

2、得到窗口的大小(WinSiez)

3、得到窗口的坐标(左下角点)

4、创建关闭按钮(Normal、Push)和回调函数

5、设置关闭按钮的位置

6、创建菜单项

7、设置菜单项的位置

8、设置菜单的位置

9、将菜单加到层中

10、创建标签

11、设置标签的位置

12、将标签加到层上

13、创建精灵

14、设置精灵的位置

15、将精灵加到层上




文章来源: yujiang.blog.csdn.net,作者:鱼酱2333,版权归原作者所有,如需转载,请联系作者。

原文链接:yujiang.blog.csdn.net/article/details/51484119

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。