cocos2d-x框架

在兄弟篇Learning Cocos2d-x for XNA1——小窥cocos2d-x框架中已有详细介绍cocos2d-x框架下的基本元素。可自行参考学习,概念的东西基本一样。

HelloWorld

HelloWorld程序虽然简单,但能测试程序是否能正确的运行,同时很能体现一个框架的整体结构。

cocos2d-x中HelloWorld显示主要通过AppDelegate和HelloWorldScene。

在显示HelloWorld程序时,得将图片资源放进Assets文件夹中

AppDelegate

C++程序中最主要的是头文件(.h)和源文件(.cpp)。

AppDelegate.h

AppDelegate.h头文件中,定义类(Class)AppDelegate继承CCApplication。在AppDelegate中声明相关方法。

AppDelegate.cpp

AppDelegate.cpp源文件中包含AppDelegate.h头文件,在其中实现头文件中声明的方法。

其中在方法applicationDidFinishLaunching中,我们找到了熟悉的CCDirector(导演),当中pScene为起始页面。很显然HelloWorld类,需要在AppDelegate.cpp中引用Classes文件夹中的HelloWorldScene.h头文件(fei话,呵呵)。

HelloWorldScene

scene()方法

主要负责将Layer层通过addChild到Scene层

init()方法

主要将Layer层以下层内容添加到Layer层。

HelloWorld::init()

 1 bool HelloWorld::init()
 2 {
 3     bool bRet = false;
 4
 5     do
 6     {
 7         if ( !CCLayer::init() )
 8         {
 9             break;
10         }
11         this->setIsTouchEnabled(true);
12
13         CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
14
15         // 1. Add a menu item with "X" image, which is clicked to quit the program.
16
17         // Create a "close" menu item with close icon, it's an auto release object.
18         CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
19             "CloseNormal.png",
20             "CloseSelected.png",
21             this,
22             menu_selector(HelloWorld::menuCloseCallback));
23         CC_BREAK_IF(! pCloseItem);
24
25         // Place the menu item bottom-right conner.
26         pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
27
28         // Create a menu with the "close" menu item, it's an auto release object.
29         CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
30         pMenu->setPosition(CCPointZero);
31         CC_BREAK_IF(! pMenu);
32
33         // Add the menu to HelloWorld layer as a child layer.
34         this->addChild(pMenu, 1);
35
36         // 2. Add a label shows "Hello World".
37
38         // Create a label and initialize with string "Hello World".
39         CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Arial", 24);
40         CC_BREAK_IF(! pLabel);
41
42         // Get window size and place the label upper.
43         CCSize size = CCDirector::sharedDirector()->getWinSize();
44         pLabel->setPosition(ccp(size.width / 2, size.height - 50));
45
46         // Add the label to HelloWorld layer as a child layer.
47         this->addChild(pLabel, 1);
48
49         // 3. Add add a splash screen, show the cocos2d splash image.
50         CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png");
51         CC_BREAK_IF(! pSprite);
52
53         // Place the sprite on the center of the screen
54         pSprite->setPosition(ccp(size.width/2, size.height/2));
55
56         // Add the sprite to HelloWorld layer as a child layer.
57         this->addChild(pSprite, 0);
58
59         bRet = true;
60     } while (0);
61
62     return bRet;
63 }

HelloWorldScene.h完整代码

HelloWorldScene.h

 1 #ifndef __HELLOWORLD_SCENE_H__
 2 #define __HELLOWORLD_SCENE_H__
 3
 4 #include "cocos2d.h"
 5 #include "SimpleAudioEngine.h"
 6
 7 class HelloWorld : public cocos2d::CCLayer
 8 {
 9 public:
10     HelloWorld();
11     ~HelloWorld();
12
13     // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
14     virtual bool init();
15
16     // there's no 'id' in cpp, so we recommand to return the exactly class pointer
17     static cocos2d::CCScene* scene();
18
19     // a selector callback
20     void menuCloseCallback(CCObject* pSender);
21
22     // implement the "static node()" method manually
23     LAYER_NODE_FUNC(HelloWorld);
24 };
25
26 #endif  // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp完整代码

HelloWorldScene.cpp

  1 #include "pch.h"
  2 #include "HelloWorldScene.h"
  3
  4 using namespace cocos2d;
  5
  6 HelloWorld::~HelloWorld()
  7 {
  8     // cpp don't need to call super dealloc
  9     // virtual destructor will do this
 10 }
 11
 12 HelloWorld::HelloWorld()
 13 {
 14 }
 15
 16 CCScene* HelloWorld::scene()
 17 {
 18     CCScene * scene = NULL;
 19     do
 20     {
 21         // 'scene' is an autorelease object
 22         scene = CCScene::node();
 23         CC_BREAK_IF(! scene);
 24
 25         // 'layer' is an autorelease object
 26         HelloWorld *layer = HelloWorld::node();
 27         CC_BREAK_IF(! layer);
 28
 29         // add layer as a child to scene
 30         scene->addChild(layer);
 31     } while (0);
 32
 33     // return the scene
 34     return scene;
 35 }
 36
 37 // on "init" you need to initialize your instance
 38 bool HelloWorld::init()
 39 {
 40     bool bRet = false;
 41
 42     do
 43     {
 44         if ( !CCLayer::init() )
 45         {
 46             break;
 47         }
 48         this->setIsTouchEnabled(true);
 49
 50         CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
 51
 52         // 1. Add a menu item with "X" image, which is clicked to quit the program.
 53
 54         // Create a "close" menu item with close icon, it's an auto release object.
 55         CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
 56             "CloseNormal.png",
 57             "CloseSelected.png",
 58             this,
 59             menu_selector(HelloWorld::menuCloseCallback));
 60         CC_BREAK_IF(! pCloseItem);
 61
 62         // Place the menu item bottom-right conner.
 63         pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
 64
 65         // Create a menu with the "close" menu item, it's an auto release object.
 66         CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
 67         pMenu->setPosition(CCPointZero);
 68         CC_BREAK_IF(! pMenu);
 69
 70         // Add the menu to HelloWorld layer as a child layer.
 71         this->addChild(pMenu, 1);
 72
 73         // 2. Add a label shows "Hello World".
 74
 75         // Create a label and initialize with string "Hello World".
 76         CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Arial", 24);
 77         CC_BREAK_IF(! pLabel);
 78
 79         // Get window size and place the label upper.
 80         CCSize size = CCDirector::sharedDirector()->getWinSize();
 81         pLabel->setPosition(ccp(size.width / 2, size.height - 50));
 82
 83         // Add the label to HelloWorld layer as a child layer.
 84         this->addChild(pLabel, 1);
 85
 86         // 3. Add add a splash screen, show the cocos2d splash image.
 87         CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png");
 88         CC_BREAK_IF(! pSprite);
 89
 90         // Place the sprite on the center of the screen
 91         pSprite->setPosition(ccp(size.width/2, size.height/2));
 92
 93         // Add the sprite to HelloWorld layer as a child layer.
 94         this->addChild(pSprite, 0);
 95
 96         bRet = true;
 97     } while (0);
 98
 99     return bRet;
100 }
101
102 void HelloWorld::menuCloseCallback(CCObject* pSender)
103 {
104     // "close" menu item clicked
105     CCDirector::sharedDirector()->end();
106 }

版本cocos2dx-0.13.0-wp8-0.8似乎不怎么给力,Lumia820真机上测试不通过,模拟器没任何问题。不过快有新版本出来了吧,现在凑合学习学习。

著作权声明:本文由http://www.cnblogs.com/suguoqiang 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

转载于:https://www.cnblogs.com/suguoqiang/archive/2013/03/14/2960376.html

Learning Cocos2d-x for WP8(2)——深入刨析Hello World相关推荐

  1. FreeRtos学习笔记(11)查找就绪任务中优先级最高任务原理刨析

    FreeRtos学习笔记(11)查找就绪任务中优先级最高任务原理刨析 怎么查找就绪任务中优先级最高的? tasks.c中声明了一个全局变量 uxTopReadyPriority,任务从其他状态进入就绪 ...

  2. spring源码刨析总结

    spring源码刨析笔记 1.概述 spring就是 spring Framework Ioc Inversion of Control(控制反转/反转控制) DI Dependancy Inject ...

  3. springMvc源码刨析笔记

    springMvc源码刨析笔记 MVC 全名是 Model View Controller,是 模型(model)-视图(view)-控制器(controller) 的缩写, 是⼀种⽤于设计创建 We ...

  4. zookeeper笔记+源码刨析

    会不断更新!冲冲冲!跳转连接 https://blog.csdn.net/qq_35349982/category_10317485.html zookeeper 1.介绍 Zookeeper 分布式 ...

  5. MapReduce源码刨析

    MapReduce编程刨析: Map map函数是对一些独立元素组成的概念列表(如单词计数中每行数据形成的列表)的每一个元素进行指定的操作(如把每行数据拆分成不同单词,并把每个单词计数为1),用户可以 ...

  6. 神经元细胞结构刨析(持续更新)

    神经元细胞结构刨析 一.简要功能组件 二. 神经元内部的成分分析 细胞核 轴突 树突 突触 线粒体 内质网 其他 三.神经元信号分类 电信号 化学信号 四.神经胶质 星形胶质细胞 少突胶质细胞 五.神 ...

  7. 2022网易最新版本将军令算法刨析(2)

    大家好,我是任雪飘!今天我们接着昨天的刨析,将一下so层的实现! 准备工作 一台安卓手机 ida 工具地址: 点击直达 提取码:w28g 网易将军令5.1.1 apk地址: 点击直达 提取码:e5wa ...

  8. 某易—将军令动态刨析算法(1)

    某易-将军令动态刨析算法(1) 我是任雪飘,一个技术渣渣,可以加我星球一起交流! 我们刷新一下,得到的就是30秒会更新一次,这里我们开始进行方法刨析. 开始记录方法调用过程 点击刷新动态码后关闭记录* ...

  9. C++异常处理机制由浅入深, 以及函数调用汇编过程底层刨析. C++11智能指针底层模拟实现

    一. 异常 1.1.异常的编程模型和基本使用 咱得用一用, 解释一下上述的模型    double Div(int a, int b) {if (b == 0) throw "Zero Di ...

  10. Metis异常检测算法率值检测和量值检测源码刨析

    Metis异常检测算法率值检测和量值检测源码刨析 1. 测试代码 2. 率值检测 2.1 rate_predict方法(detect.py) 2.2 predict方法(statistic.py) 2 ...

最新文章

  1. C++调用openssl使用sha256,并取结果前64位作为uint64
  2. ERP与EWM集成配置-ERP端组织架构(二)
  3. iOS点滴- ViewController详解
  4. 关于vue.js的部分总结
  5. zigbee协议_智能家居的ZigBee到底是什么?和Wi-Fi有何区别?
  6. 2018 总结 2019 展望
  7. Spring MVC 3.0 RESTful controller
  8. 一句话的设计模式(JAVA版)
  9. Linformer 拍了拍 被吊打 Transformers 的后浪们
  10. Java中的for循环和JavaScript中的for循环差别初探(02)
  11. 天狼星单片机c语言教程,单片机资料百度盘教程.doc
  12. HTTP请求常见错误码大全
  13. unity 自定义管线SRP 学习笔记(一)搞懂WHY WHAT HOW
  14. matlab中几种取整函数的用法(fix, floor, ceil, round)
  15. CC2430调试接口与JTAG的区别
  16. java集合比较大小_arraylist 怎么比较元素大小?
  17. 网络拓扑中,什么是核心层?什么是汇聚层?
  18. Multiclass Weighted Loss for Instance Segmentation of Cluttered Cells
  19. 计算机管理声音视屏不见,电脑视频没有声音要如何解决,戳进来看看!
  20. 计算机网络—自顶向下 计算机网络和因特网

热门文章

  1. 10 个提升效率的Linux小技巧
  2. Sketch学会这五招,快速提升工作效率!
  3. python需要多久-python培训需要多久
  4. html5 手机模板 解放区,解放区异形模板
  5. linux命令ps aux|grep xxx详解
  6. 区分Linux中的“根目录”和“家目录”
  7. 【项目实战】C/C++轻松实现4399小游戏:围住神经猫
  8. 000 我和网安的故事.doc
  9. r语言 tunerf函数_R语言 | 一网打尽高质量统计分析与机器学习包
  10. 005_解密饿了么大前端团队