cocos2d-x 2.1.4学习笔记之HelloWorld分析
下面截图是HelloWorld项目下的文件夹结构
这是用python命令生成的项目,在创建过程中默认生成多个平台的程序文件。
1.“resource”文件夹
该文件夹主要用于存放游戏中需要的图片、音频和配置等资源文件。为了方便管理,可以在其中创建子文件夹。在不同平台下,对于文件路径的定义是不一致的,但实际接口大同小异。Cocos2d-x为我们屏蔽了这些差异,其中“resource”文件夹可以默认为游戏运行时的目录。
2.proj.win32文件夹
“main.h”、“main.cpp” 用于放置游戏头文件,它们是平台相关的程序文件,为Windows专有。通常情况下,程序入口与资源文件管理在不同平台下是不同的,但是Cocos2d-x的模板已经基本为我们处理好了这些细节,不需要对它们进行修改。
3.Classes文件夹
该文件夹里面存放的是项目所有的.h和.cpp文件。自然也包含了“AppDelegate.h”和“AppDelegate.cpp”文件。这两个文件是Cocos2d-x游戏的通用入口文件,类似于一般Windows工程中主函数所在的文件。
打开“AppDelegate.cpp”,我们可以看到已经自动添加的代码,这个文件实现了AppDelegate类。AppDelegate控制着游戏的生命周期,除去构造函数和析构函数外,共有3个方法,下面逐个介绍。
AppDelegate.h
1: #ifndef _APP_DELEGATE_H_
2: #define _APP_DELEGATE_H_
3:
4: #include "cocos2d.h"
5:
6: /**
7: @brief The cocos2d Application.
8: The reason for implement as private inheritance is to hide some interface call by CCDirector.
9: */
10: class AppDelegate : private cocos2d::CCApplication {
11: public:
12: AppDelegate();
13: virtual ~AppDelegate();
14:
15: /**
16: @brief Implement CCDirector and CCScene init code here.
17: @return true Initialize success, app continue.
18: @return false Initialize failed, app terminate.
19: */
20: virtual bool applicationDidFinishLaunching();
21:
22: /**
23: @brief The function be called when the application enter background
24: @param the pointer of the application
25: */
26: virtual void applicationDidEnterBackground();
27:
28: /**
29: @brief The function be called when the application enter foreground
30: @param the pointer of the application
31: */
32: virtual void applicationWillEnterForeground();
33: };
34:
35: #endif // _APP_DELEGATE_H_
36:
AppDelegate.cpp
1: #include "AppDelegate.h"
2: #include "HelloWorldScene.h"
3:
4: USING_NS_CC;
5:
6: AppDelegate::AppDelegate() {
7: }
8:
9: AppDelegate::~AppDelegate() {
10: }
11:
12: bool AppDelegate::applicationDidFinishLaunching() {
13: //初始化游戏引擎控制器CCDirector,以便启动游戏引擎;
14: CCDirector* pDirector = CCDirector::sharedDirector();
15: CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); 16:
17: pDirector->setOpenGLView(pEGLView);
18:
19: //启用FPS显示
20: pDirector->setDisplayStats(true); 21:
22: // 设置绘制间隔(FPS); the default value is 1.0/60 if you don't call this
24: pDirector->setAnimationInterval(1.0 / 60);
25:
26: // create a scene. it's an autorelease object
27: CCScene *pScene = HelloWorld::scene();
28: // run
29: pDirector->runWithScene(pScene);
30: return true;
31: }
32:
33: // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
34: //当应用程序将要进入后台时,会调用这个方法。
35: //具体来说,当用户把程序切换到后台,或手机接到电话或短信后程序被系统切换到后台时,会调用这个方法。
36: //此时,应该暂停游戏中正在播放的音乐或音效。
37: //动作激烈的游戏通常也应该在此时进行暂停操作,以便玩家暂时离开游戏时不会遭受重大损失。
38: void AppDelegate::applicationDidEnterBackground() {
39: CCDirector::sharedDirector()->stopAnimation();
40: // if you use SimpleAudioEngine, it must be pause
41: // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
42: }
43:
44: // this function will be called when the app is active again
45: //该方法与applicationDidEnterBackground()成对出现,在应用程序回到前台时被调用。相对地,我们通常在这里继续播放刚才暂停的音乐,显示游戏暂停菜单等。
46: void AppDelegate::applicationWillEnterForeground() {
47: CCDirector::sharedDirector()->startAnimation();
48: // if you use SimpleAudioEngine, it must resume here
49: // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
50: }
51:
52: //FPS即每秒帧速率,也就是屏幕每秒重绘的次数。启用了FPS显示后,当前FPS会在游戏的左下角显示。通常在游戏开发阶段,我们会启用FPS显示,这样就可以方便地确定游戏运行是否流畅。
54: //绘制间隔指的是两次绘制的时间间隔,因此绘制间隔的倒数就是FPS上限。
55: //对于移动设备来说,我们通常都会将FPS限制在一个适当的范围内。
56: //过低的每秒重绘次数会使动画显示出卡顿的现象,而提高每秒重绘次数会导致设备运算量大幅增加,造成更高的能耗。
57: //人眼的刷新频率约为60次每秒,因此把FPS限定在60是一个较为合理的设置,Cocos2d-x就把绘制间隔设置为1/60秒。
58: //至此,我们已经完成了引擎的初始化,接下来我们将启动引擎;
HelloWorldScene.h
1: #ifndef __HELLOWORLD_SCENE_H__
2: #define __HELLOWORLD_SCENE_H__
3:
4: #include "cocos2d.h"
5:
6: class HelloWorld : public cocos2d::CCLayer {
7: public:
8: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
9: virtual bool init();
10:
11: // there's no 'id' in cpp, so we recommend returning the class instance pointer
12: //在Cocos2d中,在层下设置一个创建场景的静态函数是一个常见的技巧
13: static cocos2d::CCScene* scene();
14:
15: // a selector callback
16: void menuCloseCallback(CCObject* pSender);
17:
18: // implement the "static node()" method manually
19: CREATE_FUNC(HelloWorld);
20: };
21:
22: #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
1: #include "HelloWorldScene.h"
2:
3: USING_NS_CC;
4:
5: CCScene* HelloWorld::scene() {
6: // 'scene' is an autorelease object
7: CCScene *scene = CCScene::create();
8: // 'layer' is an autorelease object
9: HelloWorld *layer = HelloWorld::create();
10: // add layer as a child to scene
11: scene->addChild(layer);
12: // return the scene
13: return scene;
14: }
15:
16: // on "init" you need to initialize your instance
17: //为什么我们要在一个实例方法中初始化类,而不在构造函数中初始化呢?在C++中,一般习惯在构造函数中初始化类,然而由于Cocos2d-x的来源特殊,所以才没有采用C++的编程风格
18: //对父类进行初始化
19: bool HelloWorld::init() {
20: //////////////////////////////
21: // 1. super init first
22: if ( !CCLayer::init() ) {
23: return false;
24: }
25:
26: CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
27: CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
28:
29: /////////////////////////////
30: // 2. add a menu item with "X" image, which is clicked to quit the program
31: // you may modify it.
32:
33: // add a "close" icon to exit the progress. it's an autorelease object
34: CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
35: "CloseNormal.png",
36: "CloseSelected.png",
37: this,
38: menu_selector(HelloWorld::menuCloseCallback));
39:
40: pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
41: origin.y + pCloseItem->getContentSize().height/2));
42:
43: // create menu, it's an autorelease object
44: CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
45: pMenu->setPosition(CCPointZero);
46: this->addChild(pMenu, 1);
47: //addChild(CCNode* child,int zOrder),参数zOrder指的是child的z轴顺序,也就是显示的先后顺序,其值越大,表示显示的位置就越靠前
48:
49: ///////////////////////////
50: //3. add your codes below...
51: //add a label shows "Hello World"
52: //create and initialize a label
53: CCLabelTTF* plabel = CCLabelTTF::create("hello world", "Arial", 24);
54: //position the label on the center of the screen
55: plabel->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height - plabel->getContentSize().height));
56: //add the label as a child to this layer
57: this->addChild(plabel, 1);
58:
59: //add "helloworld" splash screen"
60: CCSprite* psprite = CCSprite::create("helloworld.png");
61: //position the sprite on the center of the screen
62: psprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
63: //add the sprite as a child to this layer
64: this->addChild(psprite, 0);
65: return true;
66: }
67:
68:
69: void HelloWorld::menuCloseCallback(CCObject* pSender) {
70: CCDirector::sharedDirector()->end();
71: #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
72: exit(0);
73: #endif
74: }
cocos2d-x 2.1.4学习笔记之HelloWorld分析的更多相关文章
- UML和模式应用学习笔记-1(面向对象分析和设计)
UML和模式应用学习笔记-1(面向对象分析和设计) 而只是对情节的记录:此处的用例场景为:游戏者请求掷骰子.系统展示结果:如果骰子的总点数是7,则游戏者赢得游戏,否则为输 (2)定义领域模型:在领域模 ...
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- MOOS学习笔记2——HelloWorld回调
MOOS学习笔记2--HelloWorld回调 例程 #include "MOOS/libMOOS/Comms/MOOSAsyncCommClient.h" bool OnConn ...
- MOOS学习笔记1——HelloWorld
MOOS学习笔记1--HelloWorld 例程 /* * @功能:通讯客户端的最简单程序,向MOOSDB发送名为"Greeting" * 数据"Hello", ...
- ArcGIS案例学习笔记4_2_水文分析批处理地理建模
ArcGIS案例学习笔记4_2_水文分析批处理地理建模 联系方式:谢老师,135_4855_4328,xiexiaokui#139.com 概述 计划时间:第4天下午 目的:自动化,批量化,批处理,提 ...
- ArcGIS案例学习笔记4_1_水文分析
ArcGIS案例学习笔记4_1_水文分析 联系方式:谢老师,135_4855_4328,xiexiaokui#139.com 概述 计划时间:第4天上午 教程: pdf page478 数据:实验数据 ...
- U3D学习笔记1: HelloWorld
Unity 版本: 5.3.5.f1 Hello World工程 1.新建工程 HelloWorld U3D可选2D和3D游戏 2.新建C#脚本文件 在project栏的assets目录右键-&g ...
- cocos2d-x入门学习笔记——Hello world分析
Hello world分析 1. “resource”文件夹 用于存放图片.音频和配置等资源文件.为了方便管理,可以创建在其中创建子文件夹.Cocos2d-x为我们屏蔽了不同平台对于文件路径的定义. ...
- 《A Tour of PostgreSQL Internals》学习笔记——查询处理分析
终于要迎来postgresql的<A Tour of PostgreSQL Internals>系列的最后一篇了.学习是不能拖延的事儿,越拖延事情越多.不废话,一起来看看吧~ ...
随机推荐
- ubuntu 安装mysql及修改编码
643 netstat -tap | grep mysql 645 apt-get install mysql-server mysql-client 646 netstat -tap | ...
- 计算几何模板 (bzoj 1336,poj 2451 ,poj3968)
poj 3968 (bzoj 2642) 二分+半平面交,每次不用排序,这是几个算几版综合. #include<iostream> #include<cstdio> #incl ...
- 如何使用 Cloud Insight SDK 实现 Druid 监控?
Druid 简介与用途 首先说明,这里所说的 Druid 并不是阿里巴巴的数据库连接池项目,而是 Eric Tschetter 创立的一个开源的分布式实时处理系统,希望为烧钱的大数据处理,提供一种更廉 ...
- 如何通过REST获取JENKINS的编译进度?
第二版功能需要实现, 我看了一下,获取百分比进度不太可能了,,因为JENKINS本身都没有具体的百分比进度.. 那,,只好实现获取实时值,如果完成就显示完成. URL: http://1.2.3.4/ ...
- 嵌入式C语言之---模块化编程
当你在一个项目小组做一个相对较复杂的工程时,意味着你不再独自单干.你需要和你的小组成员分工合作,一起完成项目,这就要求小组成员各自负责一部分工程.比如你可能只是负责通讯或者显示这一块.这个时候,你就应 ...
- 窗口的子类化与超类化——子类化是窗口实例级别的,超类化是在窗口类(WNDCLASS)级别的
1. 子类化 理论:子类化是这样一种技术,它允许一个应用程序截获发往另一个窗口的消息.一个应用程序通过截获属于另一个窗口的消息,从而实现增加.监视或者修改那个窗口的缺省行为.子类化是用来改变或者扩展一 ...
- 模式串匹配KMP详解
关于KMP模式串匹配网上蛮多的. 对于KMP有自己理解所以写下来希望能够对你们的学习有帮助. 之前暑假的时候学过,然后好长时间没用发现又忘了,现在再看看发现有了新的理解. ============== ...
- Maven学习一:用Maven创建Java Project
转自:http://blog.csdn.net/lfsfxy9/article/details/9399093 Maven环境配置只是入门的基础,现在要通过Maven基本命令生成一个Java Pro ...
- asp.net EasyUI DataGrid 实现增删改查
转自:http://www.cnblogs.com/create/p/3410314.html 前台代码: <!DOCTYPE html> <html xmlns="htt ...
- [转]NHibernate之旅(3):探索查询之NHibernate查询语言(HQL)
本节内容 NHibernate中的查询方法 NHibernate查询语言(HQL) 1.from子句 2.select子句 3.where子句 4.order by子句 5.group by子句 实例 ...