--------------------------CCApllication类-----------------------------
cocos2dx肯定会从CCApplication是派生出一个类,一般来说,这个派生类会重写下面3个虚函数
- //初始化scene与CCDirector
- virtual bool applicationDidFinishLaunching();
- //窗口恢复时候调用SIZE_RESTORED
- virtual void applicationWillEnterForeground();
- //窗口最小化时候调用SIZE_MINIMIZED
- virtual void applicationDidEnterBackground();
示范代码:
- bool AppDelegate::applicationDidFinishLaunching()
- {
- // initialize director
- CCDirector *pDirector = CCDirector::sharedDirector();
- pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
- CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();
- CCSize designSize = CCSizeMake(480, 320);
- if (screenSize.height > 320)
- {
- CCSize resourceSize = CCSizeMake(960, 640);
- CCFileUtils::sharedFileUtils()->setResourceDirectory("hd");
- pDirector->setContentScaleFactor(resourceSize.height/designSize.height);
- }
- CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder);
- // 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);
- CCScene * pScene = CCScene::create();
- CCLayer * pLayer = new TestController();
- pLayer->autorelease();
- pScene->addChild(pLayer);
- pDirector->runWithScene(pScene);
- return true;
- }
- // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
- void AppDelegate::applicationDidEnterBackground()
- {
- CCDirector::sharedDirector()->stopAnimation();
- SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
- SimpleAudioEngine::sharedEngine()->pauseAllEffects();
- }
- // this function will be called when the app is active again
- void AppDelegate::applicationWillEnterForeground()
- {
- CCDirector::sharedDirector()->startAnimation();
- SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
- SimpleAudioEngine::sharedEngine()->resumeAllEffects();
- }
本文详细介绍了Cocos2d-X应用开发中CCApplication类及其生命周期管理函数的应用,包括如何初始化场景、窗口恢复、窗口最小化等关键操作,并通过示范代码展示了实际实现过程。

1万+

被折叠的 条评论
为什么被折叠?



