cocos2d-x学习笔记(一)HelloCpp
2013-03-07 12:40
369 查看
main.cpp是win32平台的入口类,AppDelegate是应用真正的入口:
applicationDidFinishLaunching函数就是处理导演类和场景开始及资源的适配:
bool AppDelegate::applicationDidFinishLaunching() { // initialize director CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); pDirector->setOpenGLView(pEGLView); // Set the design resolution pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionNoBorder); CCSize frameSize = pEGLView->getFrameSize(); // In this demo, we select resource according to the frame's height. // If the resource size is different from design resolution size, you need to set contentScaleFactor. // We use the ratio of resource's height to the height of design resolution, // this can make sure that the resource's height could fit for the height of design resolution. // if the frame's height is larger than the height of medium resource size, select large resource. if (frameSize.height > mediumResource.size.height) { CCFileUtils::sharedFileUtils()->setResourceDirectory(largeResource.directory); pDirector->setContentScaleFactor(largeResource.size.height/designResolutionSize.height); } // if the frame's height is larger than the height of small resource size, select medium resource. else if (frameSize.height > smallResource.size.height) { CCFileUtils::sharedFileUtils()->setResourceDirectory(mediumResource.directory); pDirector->setContentScaleFactor(mediumResource.size.height/designResolutionSize.height); } // if the frame's height is smaller than the height of medium resource size, select small resource. else { CCFileUtils::sharedFileUtils()->setResourceDirectory(smallResource.directory); pDirector->setContentScaleFactor(smallResource.size.height/designResolutionSize.height); } // 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; }
当程序进入后台时(不活跃)会调用applicationDidEnterBackground()函数,比如在玩游戏的时候一个电话打进来,程序就会进入后台
在这里可以释放一些资源,如:停止音乐播放,停止动画等
void AppDelegate::applicationDidEnterBackground() { CCDirector::sharedDirector()->stopAnimation(); // if you use SimpleAudioEngine, it must be pause // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); }
当程序在次活跃时,会调用applicationWillEnterForeground()函数,可以在这里恢复音乐的播放,动画等
// this function will be called when the app is active again void AppDelegate::applicationWillEnterForeground() { CCDirector::sharedDirector()->startAnimation(); // if you use SimpleAudioEngine, it must resume here // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); }
HelloWorldScene场景类:
class HelloWorld : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually CREATE_FUNC(HelloWorld); };
关键的函数是init,主要是构建场景中的各种元素:如菜单项(CCMenuItem)、精灵(CCSprite)、文本(CCMenuLabel)等,每次创建元素后必须使用addChild()函数将其加入到场景中,addChild()有三个参数:1、要加入场景的对象的指针,2、绘制层的顺序,3、Tag标记。Tag标记方便用函数,getChildByTag(tag)函数取回对象,以便使用,HelloWorld的init函数必须先调用父类的init()函数:
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///////////////////////////// // 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(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE); // position the label on the center of the screen pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the label as a child to this layer this->addChild(pLabel, 1); // add "HelloWorld" splash screen" CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(pSprite, 0); return true; }
CREATE_FUNC(HelloWorld)是一个宏定义等同于:
static HelloWorld* create(){ HelloWorld* pRet = new HelloWorld(); if(pRet && pRet->init()){ pRet->autorelease(); //自动释放资源 }else{ delete pRet; pRet = NULL; } return pRet; }
void menuCloseCallback(CCObject* pSender); 是菜单项的一个回调函数,当点击菜单项就会调用此函数
// 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(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1);
CCScene* HelloWorld::scene(); 是将HelloWorld添加到场景中:
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; }
刚接触cocos2d-x,如有错误请大家纠正,共同学习进步
相关文章推荐
- Cocos2d-x学习笔记(一)HelloCpp的来龙去脉
- Android.mk文件中包含cpp文件的三种写法-(cocos2d-x 学习笔记)
- Cocos2d-x学习笔记(18)(TestCpp源代码分析-2)
- Cocos2d-x学习笔记(19)(TestCpp源代码分析-3)
- Cocos2d-x学习笔记(20)(TestCpp源代码分析-4)
- Cocos2d-x学习笔记(17)(TestCpp源码分析-1)
- Cocos2d-x学习笔记(18)(TestCpp源码分析-2)
- JSON学习笔记(格式/第三方库jsonCpp与libJson的配置和使用) cocos2d-x
- Cocos2d-x学习笔记(19)(TestCpp源码分析-3)
- Cocos2d-x学习笔记(20)(TestCpp源码分析-4)
- 【学习笔记】Cocos2d-x Lua脚本引擎之HelloLua分析笔记
- 我的Cocos2d-x学习笔记(十)定时调度器(scheduleUpdate、scheduleOnce、schedule)
- Cocos2d-x精灵学习笔记
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用
- cpp学习笔记(二)
- cocos2d-x 3.1.1 学习笔记[3]Action 动作
- cocos2d-x-3.0 Windos 新建项目(coco2d-x 学习笔记一)
- cocos2d-x 3.1.1 学习笔记[20] 像素级触摸处理
- Cocos2d-x新手学习之TestCpp项目结构分析(第四篇)[版本:cocos2d-x-3.1.1]
- Cocos2d-X 学习笔记 18 CCLayerMultiplex管理多个层