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 ...
随机推荐
- Legal or Not
Legal or Not Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- ARM学习笔记2——分支跳转指令
一.Arm指令条件码和条件助记符 二.跳转指令B 1.作用 跳转指令B使程序跳转到指定的地址执行程序(跳转范围是PC-32MB到PC+32MB) 2.指令格式(注:B后面如果有条件,条件就是紧跟在B后 ...
- GNU风格 汇编语法总结(转)
转载自:http://blog.sina.com.cn/s/blog_78d30f6b0101713r.html 汇编源程序一般用于系统最基本的初始化:初始化堆栈指针.设置页表.操作 ARM的协处理器 ...
- 17 Great Machine Learning Libraries
17 Great Machine Learning Libraries 08 October 2013 After wonderful feedback on my previous post on ...
- oracle Imp和exp以及导入常见的错误
一 1) 数据库对象已经存在 一般情况, 导入数据前应该彻底删除目标数据下的表, 序列, 函数/过程,触发器等; 数据库对象已经存在, 按缺省的imp参数, 则会导入失败 如果用了参数ignore=y ...
- SQLite 入门教程(四)增删改查,有讲究 (转)
转于: SQLite 入门教程(四)增删改查,有讲究 一.插入数据 INSERT INTO 表(列...) VALUES(值...) 根据前面几篇的内容,我们可以很轻送的创建一个数据表,并向其中插入一 ...
- text code
https://github.com/Itseez/opencv/blob/master/modules/objdetect/src/erfilter.cpp
- [D3 + AngularJS] 15. Create a D3 Chart as an Angular Directive
Integrating D3 with Angular can be very simple. In this lesson, you will learn basic integration as ...
- binary heap
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- Yet Another 10 Common Mistakes Java Developers Make When Writing SQL (You Won’t BELIEVE the Last One)--reference
(Sorry for that click-bait heading. Couldn’t resist ;-) ) We’re on a mission. To teach you SQL. But ...