日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
解析Cocos2d項目整體框架和啟動流程

Cocos2d項目整體框架和啟動流程是本文要介紹的內(nèi)容,在這里我們新建一個名為“Test2d”的項目,在xcode中的Group&Files中看到的文件結(jié)構(gòu)如下所示:

網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、成都微信小程序、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了溧陽免費建站歡迎大家使用!

cocos2d Sources:存放的是cocos2d源代碼

Classes:存放本應用程序源代碼

Other Sources:   程序的入口main函數(shù)

Resources:存放本項目的圖片、圖標、聲音文件等等

Frameworks:框架,順一下啟動流程

從main函數(shù)進入:

 
 
 
 
  1. #import     
  2.      
  3.  int main(int argc, char *argv[]) {    
  4.      NSAutoreleasePool *pool = [NSAutoreleasePool new];    
  5.     int retVal = UIApplicationMain(argc, argv, nil, @"Test2dAppDelegate");    
  6.      [pool release];    
  7.      return retVal;    
  8.  }  

第5行標識將程序的控制權(quán)傳遞給了應用代理程序?qū)ο骉est2dAppDelegate、Test2dAppDelegate

 
 
 
 
  1. Test2dAppDelegate頭文件如下   
  2.  #import     
  3.       
  4.  @interface Test2dAppDelegate : NSObject  {    
  5.      UIWindow *window;    
  6.  }    
  7.  @property (nonatomic, retain) UIWindow *window;    
  8. @end  

第三行能看出Test2dAppDelegate實現(xiàn)了系統(tǒng)定義的應用程序接口 UIApplicationDelegate

當前應用程序需要處理的各種系統(tǒng)事件:

放棄控制權(quán):applicationWillResignActive ?

獲得控制權(quán):applicationDidBecomeActive ?

內(nèi)存報警:applicationDidReceiveMemoryWarning ?

程序退出提示:applicationWillTerminate ?

系統(tǒng)時間變化:applicationSignificantTimeChange

 
 
 
 
  1. //放棄控制權(quán)    
  2. (void)applicationWillResignActive:(UIApplication *)application {    
  3. [[CCDirector sharedDirector] pause];    
  4.     
  5.      
  6. //獲得控制權(quán)    
  7. void)applicationDidBecomeActive:(UIApplication *)application {    
  8. [[CCDirector sharedDirector] resume];    
  9. }    
  10.    
  11. //內(nèi)存報警    
  12. (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {    
  13. [[CCDirector sharedDirector] purgeCachedData];    
  14.  
  15.     
  16. //    
  17. (void) applicationDidEnterBackground:(UIApplication*)application {    
  18. [[CCDirector sharedDirector] stopAnimation];    
  19. }    
  20.     //    
  21. void) applicationWillEnterForeground:(UIApplication*)application {    
  22. [[CCDirector sharedDirector] startAnimation];    
  23. }       
  24. //程序退出提示    
  25. (void)applicationWillTerminate:(UIApplication *)application {    
  26. [[CCDirector sharedDirector] end];    
  27.     
  28.     
  29. //系統(tǒng)時間變化    
  30.  (void)applicationSignificantTimeChange:(UIApplication *)application {    
  31. [[CCDirector sharedDirector] setNextDeltaTimeZero:YES];    
  32. }  

在完成刜始處理之后,通過凼數(shù) applicationDidFinishLaunching 將程序的控制權(quán)傳遞給 Cocos2D-iPhone 類庫,Cocos2D-iPhone 接下來開始準備啟勱 游戲主畫面的準備:

1.獲得主窗口對象(句柄)由成員 window 保存。

2.將 Cocos2D-iPhone 的“導演”對象與之綁定。

3. 設置“導演”對象的基本屬性。

 
 
 
 
  1. (void) applicationDidFinishLaunching:(UIApplication*)application    
  2.  {    
  3.     // CC_DIRECTOR_INIT()    
  4.     //    
  5.     // 1. Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer    
  6.     // 2. EAGLView multiple touches: disabled    
  7.      // 3. creates a UIWindow, and assign it to the "window" var (it must already be declared)    
  8.      // 4. Parents EAGLView to the newly created window    
  9.     // 5. Creates Display Link Director    
  10.      // 5a. If it fails, it will use an NSTimer director    
  11.     // 6. It will try to run at 60 FPS    
  12.      // 7. Display FPS: NO    
  13.      // 8. Device orientation: Portrait    
  14.      // 9. Connects the director to the EAGLView    
  15.      //    
  16.      CC_DIRECTOR_INIT();    
  17.              // Obtain the shared director in order to...    
  18.     CCDirector *director = [CCDirector sharedDirector];    
  19.                /***********設置“導演”對象的基本屬性***************/   
  20.       //設置主窗口方向(垂直還是水平)  
  21.       // Sets landscape mode  
  22.        [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft]; 
 
 
 
 
  1. //是否顯示FPS(每秒顯示的幀數(shù))    
  2.  // Turn on display FPS    
  3.  [director setDisplayFPS:YES];    
  4.    //設定Director對象與當前窗口的關(guān)系,便于Director操作主窗口    
  5.  // Turn on multiple touches    
  6.  EAGLView *view = [director openGLView];    
  7.  [view setMultipleTouchEnabled:YES];            
  8.  //設定主窗口顯示圖像的調(diào)色盤位寬
       // Default texture format for PNG/BMP/TIFF/JPEG/GIF images    
  9.  // It can be RGBA8888, RGBA4444, RGB***1, RGB565    
  10.  // You can change anytime.    
  11.  [CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];               
  12.      //導演對象啟動并運行場景    
  13. [[CCDirector sharedDirector] runWithScene: [HelloWorld scene]];    

Cocos2d-iPhone的主畫面對象 – HellowWorldScene 場景

場景對象 HellowWorldScence 獲得控制權(quán)后通過初始化凼數(shù) init,直接在主畫面中創(chuàng)建一個帶有“Hello world”內(nèi)容的Lable。將該標簽的位置為屏幕的中央。

 
 
 
 
  1. (id) init    
  2.  {    
  3.     // always call "super" init    
  4.     // Apple recommends to re-assign "self" with the "super" return value    
  5.      if( (self=[super init] )) {                
  6.          // create and initialize a Label    
  7.         CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];    
  8.          // ask director the the window size    
  9.          CGSize size =[[CCDirector sharedDirector] winSize];    
  10.           
  11.         // position the label on the center of the screen    
  12.          label.position =  ccp( size.width /2 , size.height/2 );                
  13.          // add the label as a child to this Layer    
  14.          [self addChild: label];    
  15.      }    
  16.      return self;    
  17.  }  

Cocos2D-iPhone 的基本導入框架就是確保 main 凼數(shù)調(diào)用正確的 應用程序代理對象。

在應用代理對象的 applicationDidFinishLaunching 凼數(shù)中:

創(chuàng)建“層“對象

將層傳遞給新創(chuàng)建的“場景“

通過“導演“對象運行新建的”場景“對象。

小結(jié):解析Cocos2d項目整體框架和啟動流程的內(nèi)容介紹完了,希望本文對你有所幫助!


本文標題:解析Cocos2d項目整體框架和啟動流程
網(wǎng)站地址:http://m.5511xx.com/article/dhsogss.html