我花了好幾天的時間尋找與此相關的信息,並將與您分享我自己的經驗。我也試圖創建一個加載到UITableViewController中的遊戲,當單元被觸摸時,CCDirector將從該遊戲加載。這是一個遊戲中心回合制遊戲,因此設計(思考與朋友說話)。我發現迄今爲止最好的方法如下(注意我在2.0中工作 - 其中CCDirector是UIViewController的子類):
在AppDelegate.h中,創建一個新的ivar以容納創建的CCGLView來自模板代碼。然後將在didFinishLaunching中創建的CCGLView分配給您的新伊娃。這允許導演重新使用最初創建的視圖,而不是在每次重新加載CCDirector時嘗試重新創建它,這似乎會導致我體驗中的各種奇怪問題。
您還想在AppDelegate中創建一個名爲-setupDirector的新方法或類似的東西,您可以在其中設置導演。這應該在每次重新創建CCDirector時調用。我已在下面發佈我的版本。請注意,CCGLView的我的ivar被稱爲「GLView」。
- (void)setupDirector {
if (![CCDirector sharedDirector]) {
CCLOG(@"Calling setupDirector");
director_ = (CCDirectorIOS*) [CCDirector sharedDirector];
director_.wantsFullScreenLayout = YES;
// Display FSP and SPF
[director_ setDisplayStats:NO];
// set FPS at 60
[director_ setAnimationInterval:1.0/60];
// attach the openglView to the director
[director_ setView:GLView];
// for rotation and other messages
[director_ setDelegate:self];
// 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");
// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change anytime.
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
// If the 1st suffix is not found and if fallback is enabled then fallback suffixes are going to searched. If none is found, it will try with the name without suffix.
// On iPad HD : "-ipadhd", "-ipad", "-hd"
// On iPad : "-ipad", "-hd"
// On iPhone HD: "-hd"
CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils];
[sharedFileUtils setEnableFallbackSuffixes:NO]; // Default: NO. No fallback suffixes are going to be used
[sharedFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"]; // Default on iPhone RetinaDisplay is "-hd"
[sharedFileUtils setiPadSuffix:@"-ipad"]; // Default on iPad is "ipad"
[sharedFileUtils setiPadRetinaDisplaySuffix:@"-ipadhd"]; // Default on iPad RetinaDisplay is "-ipadhd"
// Assume that PVR images have premultiplied alpha
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
}
此外,您還需要對模板加載視圖控制器的方式進行一些更改。通常情況下,cocos2D會以director_作爲根視圖控制器來設置導航控制器。在這裏,你想要的Alloc和初始化你的菜單視圖控制器,並添加director_的,與其:
// Create a Navigation Controller with the Director
gamesTVC_ = [[GamesTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
navController_ = [[UINavigationController alloc] initWithRootViewController:gamesTVC_];
navController_.navigationBarHidden = NO;
其他的一切在didFinishLaunching可以保持不變。現在,在你的startGameButtonPressed方法你menuViewController,您將調用新創建的setupDirector方法您的應用實例,它是通過調用引用:
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
if ([CCDirector sharedDirector].runningScene) {
[[CCDirectorIOS sharedDirector] end];
}
[app setupDirector];
[app.navController pushViewController:app.director animated:YES];
我包括檢查,以確保CCDirector沒有仍在運行如果是,就結束它。在你的遊戲層,到時候要彈出視圖控制器,你可以簡單的調用它像這樣:
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
[app.navController popViewControllerAnimated:YES];
[[CCDirector sharedDirector] end];
這種流動應該讓你自由地使用導航控制器,把你的遊戲場景CCDirector,當你想回到基於UIKit的主菜單時彈出視圖控制器。我希望這會有所幫助,因爲我花了很多時間試圖爲自己的比賽做好準備。
我真的可以使用這個問題的答案。這是我現在申請的關鍵。 – Echilon