星空网 > 软件开发 > 操作系统

【Cocos2d入门教程三】HelloWorld之一目了然

什么程序都是从HelloWorld先开始。同样Cocos2d-x我们先从HelloWorld进行下手、下面是HelloWorld的运行完成图:

【Cocos2d入门教程三】HelloWorld之一目了然images/loading.gif' data-original="http://img.blog.csdn.net/20150806105558616" />

 

建立好的Cocos游戏项目中会有两个比较常用接触的文件夹。分别为Classes与resource。Classes存取代码文件,resource存取资源文件,下面为完整的项目架构

【Cocos2d入门教程三】HelloWorld之一目了然

 

我们先来看下最基本的AppDelegate.cpp类

 1 #include "AppDelegate.h" 2 #include "HelloWorldScene.h" 3  4  5 //命名空间 6 USING_NS_CC; 7  8  9 //构造函数 10 AppDelegate::AppDelegate() { 11  12 } 13  14  15 //析构函数 16 AppDelegate::~AppDelegate()  17 { 18 } 19  20  21 //程序启动完成后会进入的函数  22 bool AppDelegate::applicationDidFinishLaunching() { 23    24   //初始化导演 25   auto director = Director::getInstance(); 26  27   //获得OpenGL视图 28   auto glview = director->getOpenGLView(); 29  30   //如果没有获取OpenGL视图 31   if(!glview)  32   { 33     //创建OpenGL视图 34     glview = GLView::create("My Game"); 35  36     //设置OpenGL视图 37     director->setOpenGLView(glview); 38   } 39  40   //设置是否显示调试信息 41   director->setDisplayStats(true); 42  43   //设置帧率 44   director->setAnimationInterval(1.0 / 60); 45  46   //调用场景 47   auto scene = HelloWorld::createScene(); 48  49   //执行场景 50   director->runWithScene(scene); 51  52   return true; 53 } 54  55  56 //当程序进入后台后调用的函数(当在玩游戏时忽然别人打来电话时,程序进入后台)  57 void AppDelegate::applicationDidEnterBackground() { 58    59   //停止播放动画 60   Director::getInstance()->stopAnimation(); 61  62   //暂停播放背景音乐 63   //SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 64 } 65  66  67 //当程序重新被激活的时候调用的函数(声音重新响起)  68 void AppDelegate::applicationWillEnterForeground() { 69    70   //播放动画 71   Director::getInstance()->startAnimation(); 72  73   //继续播放背景音乐 74   //SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 75 } 76  77  78 HelloWorld.h文件 79  80 #ifndef __HELLOWORLD_SCENE_H__ 81 #define __HELLOWORLD_SCENE_H__ 82  83 #include "cocos2d.h" 84  85 //HelloWorld类继承自Layer类 86 class HelloWorld : public cocos2d::Layer 87 { 88 public: 89   //创建场景 90   static cocos2d::Scene* createScene(); 91  92   //初始化层 93   virtual bool init();  94    95   //菜单响应函数 96   void menuCloseCallback(cocos2d::Ref* pSender); 97    98   //用于创建:场景、菜单、层等东西  99   CREATE_FUNC(HelloWorld);100 };101 102 #endif 

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

1、创建场景

2、创建层

3、将层加到场景上

4、返回场景



HelloWorld.cpp
 1 #include "HelloWorldScene.h" 2  3 //命名空间 4 USING_NS_CC; 5  6 //创建场景 7 Scene* HelloWorld::createScene() 8 { 9   //创建场景10   auto scene = Scene::create();11   12   //创建层13   auto layer = HelloWorld::create();14 15   //将层添加到场景中16   scene->addChild(layer);17 18   //返回场景19   return scene;20 }21 22 //初始化层23 bool HelloWorld::init()24 {25   //初始化父类的Layer26   if(!Layer::init())27   {28     return false;29   }30   31   //获得窗口的大小32   Size visibleSize = Director::getInstance()->getVisibleSize();33 34   //获得坐标原点的坐标35   Vec2 origin = Director::getInstance()->getVisibleOrigin();36 37   //用图片创建菜单项 38   //第一个参数:正常状态下的图片 39   //第二个参数:被选中时的图片 40   //第三个参数:响应函数 41   auto closeItem = MenuItemImage::create(42                      "CloseNormal.png",43                      "CloseSelected.png",44                      CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));45   46   //设置菜单项的位置47   closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,48                 origin.y + closeItem->getContentSize().height/2));49 50   //创建菜单51   auto menu = Menu::create(closeItem, NULL);52  53   //设置菜单的坐标原点为左下角(菜单中默认的坐标原点在窗口的中央) 54   menu->setPosition(Vec2::ZERO);55 56   //将菜单项添加到菜单中57   this->addChild(menu, 1);58 59   //创建一个标签 60   //第一个参数:标签中的内容 61   //第二个参数:字体 62   //第三个参数:字体大小 63   auto label = LabelTTF::create("Hello World", "Arial", 24);64   65   //设置标签的位置66   label->setPosition(Vec2(origin.x + visibleSize.width/2,67               origin.y + visibleSize.height - label->getContentSize().height));68 69  //设置标签的位置70   this->addChild(label, 1);71 72   //创建一个精灵73   auto sprite = Sprite::create("HelloWorld.png");74 75   //设置精灵的位置76   sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));77 78   //将精灵添加到层中79   this->addChild(sprite, 0);80   81   return true;82 }83 84 //菜单响应函数85 void HelloWorld::menuCloseCallback(Ref* pSender)86 {87 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)88   MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");89   return;90 #endif91 92   //结束场景93   Director::getInstance()->end();94 95 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)96   exit(0);97 #endif98 }


HelloWorldScene.cpp中的 HelloWorld::init(),实现了初始化:

1、初始化父类的Layer

2、得到窗口的大小

3、得到窗口的坐标

4、创建菜单项

5、设置菜单项的位置

6、设置菜单的位置

7、将菜单加到层中

8、创建标签

9、设置标签的位置

10、将标签加到层上

11、创建精灵

12、设置精灵的位置

13、将精灵加到层上

 

 1 #include "main.h" 2 #include "AppDelegate.h" 3 #include "cocos2d.h" 4  5 //命名空间 6 USING_NS_CC; 7  8 //Cocos2d-X的主函数(相当于C/C++中的main函数)  9 int APIENTRY _tWinMain(HINSTANCE hInstance,10             HINSTANCE hPrevInstance,11             LPTSTR  lpCmdLine,12            int    nCmdShow)13 {14   //表示lpCmdLine、nCmdShow是两个没用的参数 15   UNREFERENCED_PARAMETER(hPrevInstance);16   UNREFERENCED_PARAMETER(lpCmdLine);17 18   //定义一个app对象 19   AppDelegate app;20 21   //执行app对象的run函数。进入帧循环 22   return Application::getInstance()->run();23 }

 

 

main.cpp中的代码只是实现了下面的操作

定义一个App对象->执行App对象进入帧循环

一个游戏程序就这样执行起来,应运而生,是不是感觉特别的神奇。ok关于helloworld的解析就分享至此。下一章进入菜单篇的学习




 

 

 
 



原标题:【Cocos2d入门教程三】HelloWorld之一目了然

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

加拿大商标查询官网介绍商标注册的全部秘密:https://www.kjdsnews.com/a/1380808.html
加拿大商标局官网让您轻松了解商标注册流程:https://www.kjdsnews.com/a/1380809.html
加拿大商标局官网掌握商标注册的全部信息:https://www.kjdsnews.com/a/1380810.html
办公用品商标注册流程及注意事项:https://www.kjdsnews.com/a/1380811.html
办公用品商标注册哪些类别最值得关注?:https://www.kjdsnews.com/a/1380812.html
利用智能技术快速生成商标的方法:https://www.kjdsnews.com/a/1380813.html
淘宝给商家发了个“大红包” :https://www.kjdsnews.com/a/1836561.html
厦门曾厝垵家庭旅馆哪家最好?:https://www.vstour.cn/a/365183.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流