cocos2dx入门分析 hello world
打开新建的"findmistress"项目,可以看到项目文件是由多个代码文件及文件夹组成的,其中 Hello World 的代码文件直接存放于该项目文件夹中。下面我们来详细介绍一下项目的文件组成。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//初始化游戏引擎控制器 Director,以便启动游戏引擎 // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLView::create("My Game"); director->setOpenGLView(glview); }//启用 FPS 显示director->setDisplayStats(true); //设置绘制间隔 director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object auto scene = HelloWorld::createScene(); // run director->runWithScene(scene); return true; |
|
1
2
3
4
5
6
7
8
|
// 'scene' is an autorelease objectauto scene = Scene::create();// 'layer' is an autorelease objectauto layer = HelloWorld::create();// add layer as a child to scenescene->addChild(layer); |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
//(1) 对父类进行初始化 if ( !Layer::init() ) { return false; }//(2) 创建菜单并添加到层 auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 1);//(3) 创建"Hello World"标签并添加到层中 auto label = LabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen label->setPosition(Point(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label, 1);//(4) 创建显示“HelloWorld.png”的精灵并添加到层中 // add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0); |
这段代码可以简单地划分为 4 个部分。
调用父类的 init 方法来进行最初的初始化。
创建关闭程序的菜单并添加到层中。
关于菜单以及下面提到的文本标签,我们也会在后面的章节中详细介绍。
创建一个文本标签并添加到层中,显示内容"Hello World"。
用"HelloWorld.png"创建一个精灵并添加到层中。最后程序返回 true,表示初始化成功。
在 C++中,一般习惯在构造函数中初始化类,然而由于 Cocos2d-x 的来源特殊,所以才没有采用 C++的编程风格。
cocos2dx入门分析 hello world的更多相关文章
- cocos2d-x入门学习笔记,主要介绍cocos2d-x的基本结构,并且介绍引擎自带的示例
cocos2d-x 3.0 制作横版格斗游戏 http://philon.cn/post/cocos2d-x-3.0-zhi-zuo-heng-ban-ge-dou-you-xi http://blo ...
- Cocos2d-x 源代码分析 : Scheduler(定时器) 源代码分析
源代码版本号 3.1r,转载请注明 我也最终不out了,開始看3.x的源代码了.此时此刻的心情仅仅能是wtf! !!!!!!! !.只是也最终告别CC时代了. cocos2d-x 源代码分析文件夹 h ...
- cocos2d-x 源代码分析 : EventDispatcher、EventListener、Event 源代码分析 (新触摸机制,新的NotificationCenter机制)
源代码版本号来自3.x,转载请注明 cocos2d-x 源代码分析总文件夹 http://blog.csdn.net/u011225840/article/details/31743129 1.继承结 ...
- cocos2d-x 源代码分析 : control 源代码分析 ( 控制类组件 controlButton)
源代码版本号来自3.1rc 转载请注明 cocos2d-x源代码分析总文件夹 http://blog.csdn.net/u011225840/article/details/31743129 1.继承 ...
- cocos2d-x入门学习笔记——Hello world分析
Hello world分析 1. “resource”文件夹 用于存放图片.音频和配置等资源文件.为了方便管理,可以创建在其中创建子文件夹.Cocos2d-x为我们屏蔽了不同平台对于文件路径的定义. ...
- cocos2d-x 发动机分析:程序如何开始和结束?
原创地址:http://game.dapps.net/gamedev/game-engine/9515.html 感谢原创分享! 怎么样使用 Cocos2d-x 高速开发游戏,方法非常easy,你能够 ...
- Cocos2d-x性能分析-Android版本之Gprof
在 iOS 平台下我们可以用 Xcode 自带的 Profile 工具来测试我们程序的性能,Android 平台使用的 gprof 这里整理了一下具体的cocos2dx 使用gprof进行性能分析的具 ...
- cocos2d-x 源代码分析 总文件夹
这篇博客用来整理与cocos2d-x相关的工作,仅仅要有新的分析.扩展或者改动,都会更改此文章. 祝大家愉快~ 1.源代码分析 1.CCScrollView源代码分析 http://blog.csdn ...
- cocos2d-x 源代码分析 : Ref (CCObject) 源代码分析 cocos2d-x内存管理策略
从源代码版本号3.x.转载请注明 cocos2d-x 总的文件夹的源代码分析: http://blog.csdn.net/u011225840/article/details/31743129 1.R ...
随机推荐
- [转]NHibernate之旅(4):探索查询之条件查询(Criteria Query)
本节内容 NHibernate中的查询方法 条件查询(Criteria Query) 1.创建ICriteria实例 2.结果集限制 3.结果集排序 4.一些说明 根据示例查询(Query By Ex ...
- Qt学习之路(2)------Qt中的字符串类
QString QString的一些基本用法 basic.cpp #include <QTextStream> int main(void) { QTextStream out(stdou ...
- 关于ArrayList线程安全解决方案
一:使用synchronized关键字 二:使用Collections.synchronizedList();使用方法如下: 假如你创建的代码如下:List<Map<String,Obje ...
- CLR Profiler 性能分析工具 (转)
原文地址:http://www.cnblogs.com/kevinlzf/archive/2010/11/12/1876066.html 下载地址:http://www.microsoft.com/e ...
- 转载:monkeyrunner工具
前言: 最近开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括 android测试框架.CTS.Monkey.Monkeyrunner.benchmark. ...
- 【转】linux下安装opencv
Installation in Linux These steps have been tested for Ubuntu 10.04 but should work with other distr ...
- ios 中介者模式
中介设计模式在ios中普片应用于视图迁移 1,从xib中生成object对象,中介类为n个不同对象 @property(nonatomic,retain)IBOutlet NSObject *ob; ...
- 20169210《Linux内核原理与分析》课程总结
每周作业链接汇总 第一周作业:对实验楼<Linux 基础入门(新版)>课程的学习,其中有用户及文件权限管理.Linux 目录结构及文件基本操作.环境变量与文件查找.文件打包与解压缩等共17 ...
- windows mysql 中文乱码和中文录入提示太大错误的解决方法
今天操作mysql的时候很郁闷,因为修改默认字符集搞了半天,终于弄成了(关于如何把windows的默认字符集设置成功,可以参看另一篇博文,最终在mysql中输入show variables like ...
- 关于weight属性使用的一些细节
之前被这个属性困扰过好久,今天一个偶然的机会,终于把这个搞清楚了,现在与大家分享一下. 假设我们要在一个LinearLayout布局中显示两个按钮,button1和button2,button2的宽度 ...