初学试试看cocos2dx的TestCPP框架(2) --CCApllication

来源:互联网 发布:朋友借身份证开淘宝店 编辑:程序博客网 时间:2024/05/16 02:03

--------------------------CCApllication类-----------------------------

cocos2dx肯定会从CCApplication是派生出一个类,一般来说,这个派生类会重写下面3个虚函数

[cpp] view plaincopyprint?
  1. //初始化scene与CCDirector  
  2. virtual bool applicationDidFinishLaunching();  
  3. //窗口恢复时候调用SIZE_RESTORED  
  4. virtual void applicationWillEnterForeground();  
  5. //窗口最小化时候调用SIZE_MINIMIZED  
  6. virtual void applicationDidEnterBackground();  

示范代码:

[cpp] view plaincopyprint?
  1. bool AppDelegate::applicationDidFinishLaunching()  
  2. {  
  3.     // initialize director  
  4.     CCDirector *pDirector = CCDirector::sharedDirector();  
  5.     pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());  
  6.   
  7.     CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();  
  8.   
  9.     CCSize designSize = CCSizeMake(480, 320);  
  10.       
  11.     if (screenSize.height > 320)  
  12.     {  
  13.         CCSize resourceSize = CCSizeMake(960, 640);  
  14.         CCFileUtils::sharedFileUtils()->setResourceDirectory("hd");  
  15.         pDirector->setContentScaleFactor(resourceSize.height/designSize.height);  
  16.     }  
  17.   
  18.     CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder);  
  19.   
  20.     // turn on display FPS  
  21.     pDirector->setDisplayStats(true);  
  22.   
  23.     // set FPS. the default value is 1.0/60 if you don't call this  
  24.     pDirector->setAnimationInterval(1.0 / 60);  
  25.   
  26.     CCScene * pScene = CCScene::create();  
  27.     CCLayer * pLayer = new TestController();  
  28.     pLayer->autorelease();  
  29.   
  30.     pScene->addChild(pLayer);  
  31.     pDirector->runWithScene(pScene);  
  32.   
  33.     return true;  
  34. }  
  35.   
  36. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too  
  37. void AppDelegate::applicationDidEnterBackground()  
  38. {  
  39.     CCDirector::sharedDirector()->stopAnimation();  
  40.     SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  
  41.     SimpleAudioEngine::sharedEngine()->pauseAllEffects();  
  42. }  
  43.   
  44. // this function will be called when the app is active again  
  45. void AppDelegate::applicationWillEnterForeground()  
  46. {  
  47.     CCDirector::sharedDirector()->startAnimation();  
  48.     SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  
  49.     SimpleAudioEngine::sharedEngine()->resumeAllEffects();  
  50. }  

0 0
原创粉丝点击