前两天搭好了windows+VS2010+cocos2d-x 和MacMini+xcode+cocos2d-x两个开发环境。两个开发环境下的HelloWorld测试程序和自己添加的HelloWorld测试程序、

通过编译并顺利运行,具体的配置办法来自网络资料介绍,感谢强大的网路。万里长征第一步,在此Mark一下。

具体环境搭建方法网络上有许多的介绍,在此就不做复述。下面就自己对HelloWorld做具体分析。

因为Win32下和iOS下HelloWorld的结构十分相似,就那iOS下HelloWorld做举例分析。

首先程序启动进入程序入口函数,在main.m文件下

int main(int argc, char *argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];

int retVal = UIApplicationMain(argc, argv, nil, @"AppController");

[pool release];

return retVal;

}

然后进入

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions函数。在该函数中创建窗口,并调

cocos2d::CCApplication::sharedApplication()->run();该函数调用

bool AppDelegate::applicationDidFinishLaunching()函数

bool AppDelegate::applicationDidFinishLaunching()

{

// initialize director

CCDirector *pDirector = CCDirector::sharedDirector();//创建出导演类对象

pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

// turn on display FPS

pDirector->setDisplayStats(true);

// set FPS. the default value is 1.0/60 if you don't call this

pDirector->setAnimationInterval(1.0 / 60);//刷新屏幕的频率。实际上它影响到你的游戏所能达到的最大帧速率

// create a scene. it's an autorelease object

CCScene *pScene = HelloWorld::scene();//HelloWorld的scene方法实际上是创建了一个CCScene 对象(autorelease,关于autorelease属性,个人理解为类似于C++中智能指针,采用引用计数的方法管理内存)

// run

pDirector->runWithScene(pScene);

returntrue;

}

下面就HelloWorld类做点分析

在HelloWorld.h文件中有个宏定义很关键

CREATE_FUNC(HelloWorld);

它的具体代码为

#define CREATE_FUNC(__TYPE__) \

static __TYPE__* create() \

{ \

__TYPE__ *pRet = new __TYPE__(); \

if (pRet && pRet->init()) \

{ \

pRet->autorelease(); \

return pRet; \

} \

else \

{ \

delete pRet; \

pRet = NULL; \

return NULL; \

} \

}

实际上这个宏定义中会调用HelloWorld中的init方法。

CCScene* HelloWorld::scene()

{

// 'scene' is an autorelease object

CCScene *scene = CCScene::create();//关键代码,创建CCScene对象

// 'layer' is an autorelease object

HelloWorld *layer = HelloWorld::create();

// add layer as a child to scene

scene->addChild(layer);//在scene上添加layer.

// return the scene

return scene;

}

// on "init" you need to initialize your instance

boolHelloWorld::init()//layer上添加的具体精灵的实现

{

//

// 1. super init first

if ( !CCLayer::init() )

{

returnfalse;

}

/

// 2. add a menu item with "X" image, which is clicked to quit the program

//    you may modify it.

// add a "close" icon to exit the progress. it's an autorelease object

CCMenuItemImage *pCloseItem = CCMenuItemImage::create(

"CloseNormal.png",

"CloseSelected.png",

this,

menu_selector(HelloWorld::menuCloseCallback) );

pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width/2 , CCDirector::sharedDirector()->getWinSize().width/2) );

/

// 3. add your codes below..

// add a label shows "Hello World"

// create and initialize a label

CCLabelTTF* pLabel = CCLabelTTF::create("Box of chen", "Thonburi", 34);

// ask director the window size

// position the label on the center of the screen

pLabel->setPosition( ccp(CPercent::GetPercent(size.width, 50), CPercent::GetPercent(size.height, 90)) );

// add the label as a child to this layer

this->addChild(pLabel, 1);

// add "HelloWorld" splash screen"

CCSprite* pSprite = CCSprite::create("bgimg.jpg");

// position the sprite on the center of the screen

pSprite->setPosition( ccp(size.width/2, size.height/2) );

// add the sprite as a child to this layer

this->addChild(pSprite, 0);

returntrue;

}

void HelloWorld::menuCloseCallback(CCObject* pSender)

{

CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

exit(0);

#endif

}

转载于:https://www.cnblogs.com/zhidao-chen/archive/2013/03/18/2966746.html

cocos2d-x学习(一) HelloWorld相关推荐

  1. jQuery学习笔记--Helloworld

    刚学习jQuery,觉得有的东西确实有必要写下来,加深下记忆,本文纯属给自己写的,希望大家不要见笑 jQuery是什么? jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多j ...

  2. Salesforce LWC学习(二) helloWorld程序在VSCode中的实现

    上一篇我们简单的描述了一下Salesforce DX的配置以及CLI的简单功能使用,此篇主要简单描述一下LWC如何实现helloWorld以及LWC开发时应该注意的一些规范. 做国内项目的同学直观的感 ...

  3. 1.ROS编程学习:helloworld的c++与python实现

    目录 一.c++实现 1.创建工作空间 3.CMakeLists.txt配置 4.catkin_make编译 5.source一下,配置环境变量 6.roscore+rosrun 二.python实现 ...

  4. nsi学习之HelloWorld

    nsis用于打包window环境安装.卸载程序,免费开源软件(不管你用于任何用途). window环境打包是做什么? 也许你见过有的程序,copy个文件夹(或者文件),点击扩展名是.exe的文件就可以 ...

  5. cocos2d lua 学习文档

    游戏 : 2048 功能 : 开始 ,进入游戏 ,设置 ,分数系统,任务系统,金币系统 ,   待开发(背包,抽奖,签到,以及sdk和分享) 游戏源码地址: https://github.com/li ...

  6. CXF学习(2) helloworld

    0.新建一个项目取名wsserver. pom.xml 文件如下 <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...

  7. SpringMVC学习笔记——HelloWorld

    一.预备工作 创建Maven webapp工程. 使用Maven配置了需要使用的Jar包,Spring基于4.0.0版本. pom.xml如下: <project xmlns="htt ...

  8. quick cocos2d x 学习系列之三Armature

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! quic ...

  9. 深度学习之Helloworld

    文章转载自:https://blog.csdn.net/gdmj77zzh/article/details/75674463 ①前言 一.传统机器学习的回顾 人工智能是一个非常大的概念,而机器学习只是 ...

  10. lua学习笔记-HelloWorld

    2019独角兽企业重金招聘Python工程师标准>>> 运行lua脚本,lua hello.lua/dofile("hello.lua") print(" ...

最新文章

  1. cmd find 命令 多个 或者 关系 +windows_Chocolatey -Windows系统的yum||apt 软件安装工具...
  2. iOS开发网络数据之AFNetworking使用
  3. JMJS系统总结系列----Jquery分页扩展库(五)
  4. c# winform实现2048游戏
  5. 不知道被谁删了微信好友?用 Python 来帮忙呀
  6. 区块链与边缘计算(2)功能介绍
  7. validity和satisfiable的奇妙联系
  8. PHP中数组的三种排序方法
  9. jquery --- 使用when方法等待2个异步事件结束后执行某一个函数.
  10. android 可行性分析,可行性研究项目分析程序与步骤
  11. pep8 python 编码规范_如何用好python编码规范,写一手漂亮的代码
  12. 作为一名通信老司机,我是如何看待翼龙通信无人机救灾的?
  13. oracle12c 删除pdb用户,oracle 12c pdb测试:创建、开关、删除
  14. asp.net怎么实现按条件查询_【33期】分别谈谈联合索引生效和失效的条件
  15. 关于thymeleaf配置语法运用 以及 静态资源问题总结 2021-06-08
  16. 原生 Ajax 封装 和 Axios 二次 封装
  17. java项目中用了Disruptor之后,性能提升了10倍
  18. 明锐旅行车又大又便宜,值得买吗?
  19. linux在多核处理器上的负载均衡原理(2)
  20. [转] 2018年最新桌面CPU性能排行天梯图(含至强处理器)

热门文章

  1. 计算机组成原理浮点数左移规则,2020考研计算机组成原理知识点:浮点数的表示和运算...
  2. angularjs双向绑定_AngularJS隔离范围双向绑定示例
  3. ubuntu服务器设置_Ubuntu服务器设置–安全最佳实践
  4. java值的传递_Java 8可选–基于值的类使用指南
  5. spring boot缓存excel临时文件后再操作
  6. 第4周第4课:gzip、bzip2、xz
  7. Python 分析Nginx 日志并存入MySQL数据库(单线程)
  8. 未来五年的全球绿色数据中心市场的增长趋势和预测
  9. 解决tomcat 的端口被占用问题
  10. 一路风雨走过来:那些我亲密接触过的项目