Cocos2d-x学习--IOS(1)
来源:互联网 发布:yum lmysqlclient 编辑:程序博客网 时间:2024/06/10 20:32
1.Cocos2d-x是跨平台的游戏引擎。本文中使用的cocos2d-2.1rc0-x-2.1.3版本,使用Xcode4.6。
新建工程时,生成的HelloWorld解析
1.1具体的安装过程。终端进入下载文件的目录,输入 ./install-templates-xcode.sh -u
1.2Cocos2d-x有4个常用类
CCSprite、CCLayer、CCScene、CCDirector
分别是游戏中的角色(精灵),布局层,场景,导演
2.学习方式:
2.1需找其中的规律。
2.2研究引擎自带的子程序,其中的demo包容各个类能实现的效果等等
2.3主动搜索,首先推荐google
3.在创建工程的时候,会生成默认代码。
AppDelegate存在3个生命周期函数:
bool AppDelegate::applicationDidFinishLaunching(){ // initialize director CCDirector *pDirector = CCDirector::sharedDirector();//一款游戏中仅存在一个CCDirector 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(); // run pDirector->runWithScene(pScene); return true;}// This function will be called when the app is inactive. When comes a phone call,it's be invoked toovoid AppDelegate::applicationDidEnterBackground()//程序被用户切入后台运行时,会响应这个函数,可将一些数据保存等等{ CCDirector::sharedDirector()->stopAnimation(); SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); SimpleAudioEngine::sharedEngine()->pauseAllEffects();}// this function will be called when the app is active againvoid AppDelegate::applicationWillEnterForeground()//程序从后台切换到当前运行程序会响应这个函数{ CCDirector::sharedDirector()->startAnimation(); SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); SimpleAudioEngine::sharedEngine()->resumeAllEffects();}
新建工程时,生成的HelloWorld解析
class HelloWorld : public cocos2d::CCLayer{public: // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init();//初始化函数 // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d::CCScene* scene();//静态创建函数 // a selector callback void menuCloseCallback(CCObject* pSender);//menu菜单的一个回调函数 // preprocessor macro for "static create()" constructor ( node() deprecated ) CREATE_FUNC(HelloWorld);//即使老版本的create()函数};
/** * define a create function for a specific type, such as CCLayer * @__TYPE__ class type to add create(), such as CCLayer */#define CREATE_FUNC(__TYPE__) \static __TYPE__* create() \{ \//创建一个type类型的对象 __TYPE__ *pRet = new __TYPE__(); \ if (pRet && pRet->init()) \ { \//让其自动释放 pRet->autorelease(); \ return pRet; \ } \ else \ { \//如果创建失败,安全删除type类型的对象 delete pRet; \ pRet = NULL; \ return NULL; \ } \}
CCScene* HelloWorld::scene(){ // 'scene' is an autorelease object//创建一个场景对象,是一个自动释放的对象 CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer);//将布局层放入场景中 // return the scene return scene;}
bool HelloWorld::init(){ ////////////////////////////// // 1. super init first //1.先将父类初始化 if ( !CCLayer::init() ) { return false; } ///////////////////////////// // 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) );//创建Menu的菜单项 pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);//创建menu pMenu->setPosition( CCPointZero );//设置位置 this->addChild(pMenu, 1);//将mune加入布局层,z轴为1 ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34); // ask director the window size CCSize size = CCDirector::sharedDirector()->getWinSize(); // position the label on the center of the screen pLabel->setPosition( ccp(size.width / 2, size.height - 20) ); // add the label as a child to this layer this->addChild(pLabel, 1);//将HelloWorld的Label加入布局层,在z轴上和menu同一层面 // add "HelloWorld" splash screen" CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // 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); return true;}void HelloWorld::menuCloseCallback(CCObject* pSender){ CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)//判断当前设备是否为ios设备 exit(0);#endif}
- Cocos2d-x学习--IOS(1)
- cocos2d-x 学习 -ios
- Cocos2d-x学习--Ios(2)
- [Cocos2D 2.x iOS学习笔记1]启程
- iOS之cocos2d和cocos2d-x的学习
- Cocos2d-x学习(1) - cocos2d环境配置
- 学习cocos2d-x笔记-1
- cocos2d-x 学习笔记1
- cocos2d-x 学习笔记1
- Cocos2d-x学习(1)
- Cocos2d-x学习笔记1
- Cocos2d-x学习笔记1
- 学习1--Cocos2d-x概要
- cocos2d-x学习笔记(1)
- cocos2d-x 学习点滴(1) 初识cocos2d-x
- 学习cocos2d-x 之路 (1)--了解cocos2d-x
- cocos2d-x-LuaProxy学习日志(1) -- Cocos2d-x HelloLua 介绍
- cocos2d-x-LuaProxy学习日志(1) -- Cocos2d-x HelloLua 介绍
- 埃及为何越来越不喜欢美国
- MS VC 自然对齐 声明示例
- QT 自定义调试信息的输出
- poj 1011 Sticks (DFS)
- Windows下tomcat jvm参数设定
- Cocos2d-x学习--IOS(1)
- ubuntu解压windows生成的zip文件时乱码问题
- compareTo 排序
- 滚动条问题--CSrollView
- C++中const限定符的一些用法
- [VS2005]解决“由于应用程序的配置不正确,应用程序未能启动,重新安装应用程序可能会纠正这个问题”
- wdOS系统上checkinstall工具的安装和应用示例
- 开发phonegap应用问题汇总
- 很久没有应用IIS了突然运用到后感觉到陌生