解析Cocos2d項目整體框架和啟動流程
Cocos2d項目整體框架和啟動流程是本文要介紹的內容,在這里我們新建一個名為“Test2d”的項目,在xcode中的Group&Files中看到的文件結構如下所示:
cocos2d Sources:存放的是cocos2d源代碼
Classes:存放本應用程序源代碼
Other Sources: 程序的入口main函數
Resources:存放本項目的圖片、圖標、聲音文件等等
Frameworks:框架,順一下啟動流程
從main函數進入:
- #import <UIKit/UIKit.h>
- int main(int argc, char *argv[]) {
- NSAutoreleasePool *pool = [NSAutoreleasePool new];
- int retVal = UIApplicationMain(argc, argv, nil, @"Test2dAppDelegate");
- [pool release];
- return retVal;
- }
第5行標識將程序的控制權傳遞給了應用代理程序對象Test2dAppDelegate、Test2dAppDelegate
- Test2dAppDelegate<SPAN style="FONT-SIZE: 14px; LINE-HEIGHT: 21px; FONT-FAMILY: verdana, 'courier new'; WHITE-SPACE: normal">頭文件如下</SPAN>
- #import <UIKit/UIKit.h>
- @interface Test2dAppDelegate : NSObject <UIApplicationDelegate> {
- UIWindow *window;
- }
- @property (nonatomic, retain) UIWindow *window;
- @end
第三行能看出Test2dAppDelegate實現了系統定義的應用程序接口 UIApplicationDelegate
當前應用程序需要處理的各種系統事件:
放棄控制權:applicationWillResignActive
獲得控制權:applicationDidBecomeActive
內存報警:applicationDidReceiveMemoryWarning
程序退出提示:applicationWillTerminate
系統時間變化:applicationSignificantTimeChange
- //放棄控制權
- (void)applicationWillResignActive:(UIApplication *)application {
- [[CCDirector sharedDirector] pause];
- //獲得控制權
- void)applicationDidBecomeActive:(UIApplication *)application {
- [[CCDirector sharedDirector] resume];
- }
- //內存報警
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
- [[CCDirector sharedDirector] purgeCachedData];
- //
- (void) applicationDidEnterBackground:(UIApplication*)application {
- [[CCDirector sharedDirector] stopAnimation];
- }
- //
- void) applicationWillEnterForeground:(UIApplication*)application {
- [[CCDirector sharedDirector] startAnimation];
- }
- //程序退出提示
- (void)applicationWillTerminate:(UIApplication *)application {
- [[CCDirector sharedDirector] end];
- //系統時間變化
- (void)applicationSignificantTimeChange:(UIApplication *)application {
- [[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
- }
在完成刜始處理之后,通過凼數 applicationDidFinishLaunching 將程序的控制權傳遞給 Cocos2D-iPhone 類庫,Cocos2D-iPhone 接下來開始準備啟勱 游戲主畫面的準備:
1.獲得主窗口對象(句柄)由成員 window 保存。
2.將 Cocos2D-iPhone 的“導演”對象與之綁定。
3. 設置“導演”對象的基本屬性。
- (void) applicationDidFinishLaunching:(UIApplication*)application
- {
- // CC_DIRECTOR_INIT()
- //
- // 1. Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer
- // 2. EAGLView multiple touches: disabled
- // 3. creates a UIWindow, and assign it to the "window" var (it must already be declared)
- // 4. Parents EAGLView to the newly created window
- // 5. Creates Display Link Director
- // 5a. If it fails, it will use an NSTimer director
- // 6. It will try to run at 60 FPS
- // 7. Display FPS: NO
- // 8. Device orientation: Portrait
- // 9. Connects the director to the EAGLView
- //
- CC_DIRECTOR_INIT();
- // Obtain the shared director in order to...
- CCDirector *director = [CCDirector sharedDirector];
- /***********設置“導演”對象的基本屬性***************/
- //設置主窗口方向(垂直還是水平)
- // Sets landscape mode
- [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
- //是否顯示FPS(每秒顯示的幀數)
- // Turn on display FPS
- [director setDisplayFPS:YES];
- //設定Director對象與當前窗口的關系,便于Director操作主窗口
- // Turn on multiple touches
- EAGLView *view = [director openGLView];
- [view setMultipleTouchEnabled:YES];
- //設定主窗口顯示圖像的調色盤位寬<BR> // Default texture format for PNG/BMP/TIFF/JPEG/GIF images
- // It can be RGBA8888, RGBA4444, RGB***1, RGB565
- // You can change anytime.
- [CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
- //導演對象啟動并運行場景
- [[CCDirector sharedDirector] runWithScene: [HelloWorld scene]];
Cocos2d-iPhone的主畫面對象 – HellowWorldScene 場景
場景對象 HellowWorldScence 獲得控制權后通過初始化凼數 init,直接在主畫面中創建一個帶有“Hello world”內容的Lable。將該標簽的位置為屏幕的中央。
- (id) init
- {
- // always call "super" init
- // Apple recommends to re-assign "self" with the "super" return value
- if( (self=[super init] )) {
- // create and initialize a Label
- CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
- // ask director the the window size
- CGSize size = [[CCDirector sharedDirector] winSize];
- // position the label on the center of the screen
- label.position = ccp( size.width /2 , size.height/2 );
- // add the label as a child to this Layer
- [self addChild: label];
- }
- return self;
- }
Cocos2D-iPhone 的基本導入框架就是確保 main 凼數調用正確的 應用程序代理對象。
在應用代理對象的 applicationDidFinishLaunching 凼數中:
創建“層“對象
將層傳遞給新創建的“場景“
通過“導演“對象運行新建的”場景“對象。
小結:解析Cocos2d項目整體框架和啟動流程的內容介紹完了,希望本文對你有所幫助!