cocos2dx学习笔记(HelloWorld解析)

本文详细介绍了使用Cocos2d-x游戏引擎开发的游戏启动流程,包括AppDelegate类中的关键函数applicationDidFinishLaunching,该函数负责初始化游戏引擎、设置帧率、创建并启动主场景等。此外还涉及了应用进入后台和重新激活时的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:http://blog.youkuaiyun.com/qq_17749439/article/details/37907161

入口函数AppDelegate.cpp

[cpp]  view plain copy
  1. bool AppDelegate::applicationDidFinishLaunching() {  
  2.     // initialize director  
  3.     //初始化游戏引擎控制器CCDirector,以便启动游戏引擎,单例对象  
  4.     CCDirector* pDirector = CCDirector::sharedDirector();  
  5.     CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();  
  6.   
  7.     pDirector->setOpenGLView(pEGLView);  
  8.       
  9.     // turn on display FPS  
  10.     //打开fps  
  11.     pDirector->setDisplayStats(true);  
  12.   
  13.     // set FPS. the default value is 1.0/60 if you don't call this  
  14.     //设置绘制间隔为1/60每秒  
  15.     pDirector->setAnimationInterval(1.0 / 60);  
  16.   
  17.     // create a scene. it's an autorelease object  
  18.     //创建helloworld场景  
  19.     CCScene *pScene = HelloWorld::scene();  
  20.   
  21.     // run  
  22.     //启动引擎  
  23.     pDirector->runWithScene(pScene);  
  24.   
  25.     return true;  
  26. }  
  27.   
  28. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too  
  29. void AppDelegate::applicationDidEnterBackground() {  
  30.     //当应用程序将要进入后台时,调用  
  31.     CCDirector::sharedDirector()->stopAnimation();  
  32.   
  33.     // if you use SimpleAudioEngine, it must be pause  
  34.     // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  
  35. }  
  36.   
  37. // this function will be called when the app is active again  
  38. void AppDelegate::applicationWillEnterForeground() {  
  39.     //当应用程序回到前台时调用  
  40.     CCDirector::sharedDirector()->startAnimation();  
  41.   
  42.     // if you use SimpleAudioEngine, it must resume here  
  43.     // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  
  44. }  
HelloWorld.cpp函数

[cpp]  view plain copy
  1. CCScene* HelloWorld::scene()  
  2. {  
  3.     // 'scene' is an autorelease object  
  4.     CCScene *scene = CCScene::create();  
  5.       
  6.     // 'layer' is an autorelease object  
  7.     HelloWorld *layer = HelloWorld::create();  
  8.   
  9.     // add layer as a child to scene  
  10.     //调用scene对象的addChild方法来吧创建的层加到场景之中  
  11.     scene->addChild(layer);  
  12.   
  13.     // return the scene  
  14.     return scene;  
  15. }  
  16.   
  17. // on "init" you need to initialize your instance  
  18. bool HelloWorld::init()  
  19. {  
  20.     //////////////////////////////  
  21.     // 1. super init first  
  22.     //对父类进行初始化  
  23.     if ( !CCLayer::init() )  
  24.     {  
  25.         return false;  
  26.     }  
  27.     //获取可视区域大小  
  28.     CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();  
  29.     //获取可视区域左下角坐标  
  30.     CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();  
  31.   
  32.     /////////////////////////////  
  33.     // 2. add a menu item with "X" image, which is clicked to quit the program  
  34.     //    you may modify it.  
  35.   
  36.     // add a "close" icon to exit the progress. it's an autorelease object  
  37.     //创建菜单项的图像  
  38.     CCMenuItemImage *pCloseItem = CCMenuItemImage::create(  
  39.                                         "CloseNormal.png",  
  40.                                         "CloseSelected.png",  
  41.                                         this,  
  42.                                         menu_selector(HelloWorld::menuCloseCallback));  
  43.     //将菜单项的图像放在右下角  
  44.     pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,  
  45.                                 origin.y + pCloseItem->getContentSize().height/2));  
  46.   
  47.     // create menu, it's an autorelease object  
  48.     //将菜单项的图像加入菜单  
  49.     CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);  
  50.     pMenu->setPosition(CCPointZero);  
  51. //    将菜单加入HelloWorld层中  
  52.     this->addChild(pMenu, 1);  
  53.   
  54.     /////////////////////////////  
  55.     // 3. add your codes below...  
  56.   
  57.     // add a label shows "Hello World"  
  58.     // create and initialize a label  
  59. //    创建字体对象Hello World  
  60.     CCLabelTTF* pLabel = CCLabelTTF::create("Hello World""Arial", 24);  
  61.       
  62.     // position the label on the center of the screen  
  63. //    将字体放在正上角  
  64.     pLabel->setPosition(ccp(origin.x + visibleSize.width/2,  
  65.                             origin.y + visibleSize.height - pLabel->getContentSize().height));  
  66.   
  67.     // add the label as a child to this layer  
  68. //    将字体加入HelloWorld层中  
  69.     this->addChild(pLabel, 1);  
  70.   
  71.     // add "HelloWorld" splash screen"  
  72. //创建HelloWorld.png精灵对象  
  73.     CCSprite* pSprite = CCSprite::create("HelloWorld.png");  
  74.   
  75.     // position the sprite on the center of the screen  
  76. //    将HelloWorld.png精灵对象位于正中央  
  77.     pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));  
  78.   
  79.     // add the sprite as a child to this layer  
  80. //    将HelloWorld.png精灵对象加入HelloWorld层中  
  81.     this->addChild(pSprite, 0);  
  82.       
  83.     return true;  
  84. }  
  85.   
  86. //回调函数,关闭场景  
  87. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  88. {  
  89. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)  
  90.     CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");  
  91. #else  
  92.     CCDirector::sharedDirector()->end();  
  93. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  94.     exit(0);  
  95. #endif  
  96. #endif  
  97. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值