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

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、成都微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了溧陽免費(fèi)建站歡迎大家使用!
cocos2d Sources:存放的是cocos2d源代碼
Classes:存放本應(yīng)用程序源代碼
Other Sources: 程序的入口main函數(shù)
Resources:存放本項(xiàng)目的圖片、圖標(biāo)、聲音文件等等
Frameworks:框架,順一下啟動(dòng)流程
從main函數(shù)進(jìn)入:
- #import
- int main(int argc, char *argv[]) {
- NSAutoreleasePool *pool = [NSAutoreleasePool new];
- int retVal = UIApplicationMain(argc, argv, nil, @"Test2dAppDelegate");
- [pool release];
- return retVal;
- }
第5行標(biāo)識將程序的控制權(quán)傳遞給了應(yīng)用代理程序?qū)ο骉est2dAppDelegate、Test2dAppDelegate
- Test2dAppDelegate頭文件如下
- #import
- @interface Test2dAppDelegate : NSObject
{ - UIWindow *window;
- }
- @property (nonatomic, retain) UIWindow *window;
- @end
第三行能看出Test2dAppDelegate實(shí)現(xiàn)了系統(tǒng)定義的應(yīng)用程序接口 UIApplicationDelegate
當(dāng)前應(yīng)用程序需要處理的各種系統(tǒng)事件:
放棄控制權(quán):applicationWillResignActive ?
獲得控制權(quán):applicationDidBecomeActive ?
內(nèi)存報(bào)警:applicationDidReceiveMemoryWarning ?
程序退出提示:applicationWillTerminate ?
系統(tǒng)時(shí)間變化:applicationSignificantTimeChange
- //放棄控制權(quán)
- (void)applicationWillResignActive:(UIApplication *)application {
- [[CCDirector sharedDirector] pause];
- //獲得控制權(quán)
- void)applicationDidBecomeActive:(UIApplication *)application {
- [[CCDirector sharedDirector] resume];
- }
- //內(nèi)存報(bào)警
- (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];
- //系統(tǒng)時(shí)間變化
- (void)applicationSignificantTimeChange:(UIApplication *)application {
- [[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
- }
在完成刜始處理之后,通過凼數(shù) applicationDidFinishLaunching 將程序的控制權(quán)傳遞給 Cocos2D-iPhone 類庫,Cocos2D-iPhone 接下來開始準(zhǔn)備啟勱 游戲主畫面的準(zhǔn)備:
1.獲得主窗口對象(句柄)由成員 window 保存。
2.將 Cocos2D-iPhone 的“導(dǎo)演”對象與之綁定。
3. 設(shè)置“導(dǎo)演”對象的基本屬性。
- (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];
- /***********設(shè)置“導(dǎo)演”對象的基本屬性***************/
- //設(shè)置主窗口方向(垂直還是水平)
- // Sets landscape mode
- [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
- //是否顯示FPS(每秒顯示的幀數(shù))
- // Turn on display FPS
- [director setDisplayFPS:YES];
- //設(shè)定Director對象與當(dāng)前窗口的關(guān)系,便于Director操作主窗口
- // Turn on multiple touches
- EAGLView *view = [director openGLView];
- [view setMultipleTouchEnabled:YES];
- //設(shè)定主窗口顯示圖像的調(diào)色盤位寬
// 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];
- //導(dǎo)演對象啟動(dòng)并運(yùn)行場景
- [[CCDirector sharedDirector] runWithScene: [HelloWorld scene]];
Cocos2d-iPhone的主畫面對象 – HellowWorldScene 場景
場景對象 HellowWorldScence 獲得控制權(quán)后通過初始化凼數(shù) init,直接在主畫面中創(chuàng)建一個(gè)帶有“Hello world”內(nèi)容的Lable。將該標(biāo)簽的位置為屏幕的中央。
- (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 的基本導(dǎo)入框架就是確保 main 凼數(shù)調(diào)用正確的 應(yīng)用程序代理對象。
在應(yīng)用代理對象的 applicationDidFinishLaunching 凼數(shù)中:
創(chuàng)建“層“對象
將層傳遞給新創(chuàng)建的“場景“
通過“導(dǎo)演“對象運(yùn)行新建的”場景“對象。
小結(jié):解析Cocos2d項(xiàng)目整體框架和啟動(dòng)流程的內(nèi)容介紹完了,希望本文對你有所幫助!
本文題目:解析Cocos2d項(xiàng)目整體框架和啟動(dòng)流程
文章鏈接:http://fisionsoft.com.cn/article/dhsogss.html


咨詢
建站咨詢
