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

移动开发 iOS 游戏开发
本文介绍的是解析Cocos2d项目整体框架和启动流程,主要介绍了要处理的各种系统事件!

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

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

cocos2d Sources:存放的是cocos2d源代码

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

Other Sources:   程序的入口main函数

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

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

从main函数进入:

  1. #import <UIKit/UIKit.h>    
  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<SPAN style="FONT-SIZE: 14px; LINE-HEIGHT: 21px; FONT-FAMILY: verdana, 'courier new'; WHITE-SPACE: normal">头文件如下</SPAN>   
  2.  #import <UIKit/UIKit.h>    
  3.       
  4.  @interface Test2dAppDelegate : NSObject <UIApplicationDelegate> {    
  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.  //设定主窗口显示图像的调色盘位宽<BR>   // 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项目整体框架和启动流程的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-08-08 17:17:55

Cocos2D 坐标 OpenglES

2012-06-01 10:27:44

Cocos2d触摸分发原理

2011-08-11 17:52:01

Cocos2d游戏对象

2011-07-27 13:57:36

iPhone 游戏 Cocos2d

2011-08-08 15:40:47

Cocos2d

2011-07-27 10:13:23

Cocos2D iPhone

2011-08-11 18:00:18

Cocos2d动作Action

2011-07-29 18:02:06

2012-02-19 20:10:23

Cocos2d-x fCocos2dWindows Pho

2011-08-02 15:37:48

Cocos2D UIAccelero

2011-07-20 14:04:46

Cocos2d iPhone 游戏

2011-07-27 13:44:08

2011-08-09 16:25:16

Cocos2d视图坐标

2011-08-08 11:26:39

Cocos2d 游戏 Class类

2011-07-27 14:48:21

iPhone Cocos2D 坐标

2011-08-11 14:22:47

iPhone游戏Cocos2D

2011-08-04 17:01:16

iPhone游戏开发 Cocos2d

2011-08-02 15:47:28

Cocos2D Animation

2011-07-08 16:09:54

Cocoa Cocos2d 动作

2011-08-08 11:40:42

Cocos2d CCLayer Touch
点赞
收藏

51CTO技术栈公众号