解析Cocos2d项目整体框架和启动流程

Cocos2d项目整体框架和启动流程是本文要介绍的内容,在这里我们新建一个名为“Test2d”的项目,在xcode中的Group&Files中看到的文件结构如下所示:

网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、成都微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了溧阳免费建站欢迎大家使用!

cocos2d Sources:存放的是cocos2d源代码

Classes:存放本应用程序源代码

Other Sources:   程序的入口main函数

Resources:存放本项目的图片、图标、声音文件等等

Frameworks:框架,顺一下启动流程

从main函数进入:

 
 
 
 
  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行标识将程序的控制权传递给了应用代理程序对象Test2dAppDelegate、Test2dAppDelegate

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

第三行能看出Test2dAppDelegate实现了系统定义的应用程序接口 UIApplicationDelegate

当前应用程序需要处理的各种系统事件:

放弃控制权:applicationWillResignActive 

获得控制权:applicationDidBecomeActive 

内存报警:applicationDidReceiveMemoryWarning 

程序退出提示:applicationWillTerminate 

系统时间变化:applicationSignificantTimeChange

 
 
 
 
  1. //放弃控制权    
  2. (void)applicationWillResignActive:(UIApplication *)application {    
  3. [[CCDirector sharedDirector] pause];    
  4.     
  5.      
  6. //获得控制权    
  7. void)applicationDidBecomeActive:(UIApplication *)application {    
  8. [[CCDirector sharedDirector] resume];    
  9. }    
  10.    
  11. //内存报警    
  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. //系统时间变化    
  30.  (void)applicationSignificantTimeChange:(UIApplication *)application {    
  31. [[CCDirector sharedDirector] setNextDeltaTimeZero:YES];    
  32. }  

在完成刜始处理之后,通过凼数 applicationDidFinishLaunching 将程序的控制权传递给 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(每秒显示的帧数)    
  2.  // Turn on display FPS    
  3.  [director setDisplayFPS:YES];    
  4.    //设定Director对象与当前窗口的关系,便于Director操作主窗口    
  5.  // Turn on multiple touches    
  6.  EAGLView *view = [director openGLView];    
  7.  [view setMultipleTouchEnabled:YES];            
  8.  //设定主窗口显示图像的调色盘位宽
       // 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 获得控制权后通过初始化凼数 init,直接在主画面中创建一个带有“Hello world”内容的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 凼数调用正确的 应用程序代理对象。

在应用代理对象的 applicationDidFinishLaunching 凼数中:

创建“层“对象

将层传递给新创建的“场景“

通过“导演“对象运行新建的”场景“对象。

小结:解析Cocos2d项目整体框架和启动流程的内容介绍完了,希望本文对你有所帮助!

网站名称:解析Cocos2d项目整体框架和启动流程
当前地址:http://www.csdahua.cn/qtweb/news27/506477.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网