您的代碼設置似乎有點怪異。您正在UIView上加載cocos2d,但在該視圖上保留了導演(ViewController)。在ViewController結構中啓動和結束cocos2d引擎可能有點棘手,但這是我在我的遊戲中使用的:
步驟1:從模板中修改AppDelegate.m中的標準代碼。您需要註釋掉所有與_director ivar相關的行,並將根視圖控制器從_director更改爲您的ViewController。現在遊戲將在cocos2d模板代碼中創建的navController中啓動到該ViewController中,而不是_director。
步驟2:你推出從需求的cocos2d有一個被稱爲在創建和,這是很重要的-init方法的視圖控制器,保留,導演用作其觀點,像這樣的CCGLView :
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
myGLView = [[CCGLView viewWithFrame:[app.window bounds]
pixelFormat:kEAGLColorFormatRGB565 //kEAGLColorFormatRGBA8
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0] retain];
保持周圍的CCGLView是防止一些OpenGL的錯誤很重要,因爲cocos2d的似乎有重新創建它被摧毀後的問題。就像我所說的那樣,這個方法只會在你啓動cocos2d的ViewController的-init方法中被調用一次。
第3步:在您的視圖控制器設置主管創建一個方法,並將它推到導航控制器的堆棧,像這樣:
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
CCDirectorIOS* director = (CCDirectorIOS *) [CCDirector sharedDirector];
director.wantsFullScreenLayout = YES;
[director setView:myGLView];
// Display FSP and SPF
[director setDisplayStats:NO];
// set FPS at 60
[director setAnimationInterval:1.0/60];
// for rotation and other messages
[director setDelegate:app];
// 2D projection
[director setProjection:kCCDirectorProjection2D];
// [director setProjection:kCCDirectorProjection3D];
// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if(! [director enableRetinaDisplay:YES])
CCLOG(@"Retina Display Not supported");
if (director.runningScene)
[director replaceScene:[TwoPlayerBoard node]];
else
[director pushScene:[TwoPlayerBoard node]];
[app.navController pushViewController:director animated:YES];
該代碼的絕大部分是從註釋掉的代碼在AppDelegate中實際複製最初的導演應該已經設置好了。請注意,您將導演視圖設置爲您在ViewController的-init中創建並保留的CCGLView。在這種方法中,你有新創建的導演推動你的創業場景。
第4步:遊戲層,只要你想回到你的ViewController的遊戲(cocos2d的)從啓動裏面,實現這個代碼:
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
[app.navController popViewControllerAnimated:YES];
[[CCDirectorIOS sharedDirector] end];
這應該讓你從一個自由移動視圖控制器,給cocos2d(也是UIViewController的一個子類)的導演,並返回沒有任何問題。希望能夠爲您詳細解釋它,讓我知道它是如何發展的!
你的問題不清楚。你可以把你的錯誤堆棧和/或更多的代碼來幫助理解你的問題嗎? – 2012-07-28 16:46:35