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>系列的最后一篇了.学习是不能拖延的事儿,越拖延事情越多.不废话,一起来看看吧~ ...
随机推荐
- IE8下String的Trim()方法失效的解决方法
String的Trim()方法失效,在ie8下是有这样的情况的,解决方法也很简单使用$.trim(str)即可,需要的朋友可以了解下 用jquery的trim()方法,$.trim(str)就可以了.
- SCU3502 The Almost Lucky Number
Description A lucky number is a number whose decimal representation contains only the digits \(4\) a ...
- VC提交网页表单(一共八篇)
VC提交网页表单-自动评论留言(1)http://blog.csdn.net/wangningyu/article/details/4526357VC提交网页表单-自动评论留言(2)http://bl ...
- CKFinder 2.4 ASP.NET 破解
网上的破解教程对于2.4版本来说已经过时了. 以下是CKFinder 2.4 ASP.NET的亲测可用破解方法,经测试,只需修改两处代码. 打开ckfinder.js, 步骤1. 搜索替换如下代码: ...
- 子查询解嵌套not in 无法展开改写
SQL> explain plan for select * from OPS$CZTEST1.SAVJ_ATOMJOURBAK where ((list_flag = '1' and prt_ ...
- Android中的常见时区
方法: private void printTimeZone(){ String[] ids= TimeZone.getAvailableIDs(); for (int i = 0; i < i ...
- 【CF】135 Div2 Choosing Capital for Treeland
树形结构,挺有意思的题目.不难. /* 219D */ #include <iostream> #include <string> #include <map> # ...
- linux 复制文件时,报cp: omitting directory `XXX'
今天在用linux命令进行文件复制时omitting cp -i BBS /opt/workspace/apache-tomcat-6,参数用的是 -i),所以也不太熟悉,原来,还有子目录文件,而是必 ...
- 漫谈CSS的渲染效率
总结了部分所学.所听.所看.所问的一些CSS写作经验,书写高效的CSS - 漫谈CSS的渲染效率,它们与渲染效率及所占用消耗的资源有一定的关 联.部分为自己理解所写,不排除会有错漏,欢迎提供更好的意见 ...
- (转载)顺序栈c++实现
(转载)http://myswirl.blog.163.com/blog/static/51318642200882310239324/ SqStack.h********************** ...