cocos2d 2.0-rc2:结束导演并重启

我有一个使用UIKit菜单的cocos2d动力游戏,所以我只使用一个viewcontroller的框架,这是游戏本身。 而且,它只有一个场景。 由于cocos2d 2.0导演本身是一个UIViewController子类,所以当用户点击一个开始button时,我只是将它推入我的MenuViewController

 -(void)startGameButtonPressed { CCDirectorIOS* director = (CCDirectorIOS *) [CCDirector sharedDirector]; // Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits self.glView = [CCGLView viewWithFrame:CGRectMake(0, 0, 480, 320) pixelFormat:kEAGLColorFormatRGB565 //kEAGLColorFormatRGBA8 depthFormat:0 //GL_DEPTH_COMPONENT24_OES preserveBackbuffer:NO sharegroup:nil multiSampling:NO numberOfSamples:0]; // attach the openglView to the director [director setView:glView]; [director runWithScene:[GameLayer scene]]; [director setDelegate:(id <CCDirectorDelegate>) [[UIApplication sharedApplication] delegate]]; [self.navigationController pushViewController:director animated:YES]; } 

当用户启动第一个游戏时,这个方法第一次被调用就可以正常工作。 游戏结束后,我打电话给[[CCDirector sharedDirector] end]

大部分的导演设置都是在appDelegate中完成的(这是从默认的Cocos2d模板中得到的)。 我只把CCGLView作为一个保留的属性到我的MenuViewController ,否则当调用[[CCDirector sharedDirector] end]时应用程序崩溃,并且CCGLView不被保留。 我认为这可能是一个cocos2d错误。 在[[CCDirector sharedDirector] end]框架调用[self setView:nil] ,但它仍然试图访问视图(可能在另一个线程)。

现在的问题是,在我上面的方法的第二个电话(当用户想要从菜单开始另一个游戏时), startGameButtonPressed ,导演被推动,但屏幕保持黑屏。 游戏正在运行和响应,我只是没有看到任何东西。 有人可以帮我一下吗?

好吧,我有同样的问题,我可以“修复”。

当您设置CCGLView和[director setView:]时,即使popup控制器,场景仍然存在。 唯一发生的事情是现场被停止。

所以为了让“重新启动”工作,你必须检查是否已经有一个正在运行的场景(即使停止了,而不是runWithScene:你使用replaceScene: runWithScene:

这是我的代码,所以你可以看到:

 - (void)setupCocos2D { CCGLView *glView = [CCGLView viewWithFrame:CGRectMake(0, 0, 320, 480) pixelFormat:kEAGLColorFormatRGB565 //kEAGLColorFormatRGBA8 depthFormat:0 //GL_DEPTH_COMPONENT24_OES preserveBackbuffer:NO sharegroup:nil multiSampling:NO numberOfSamples:0]; // HERE YOU CHECK TO SEE IF THERE IS A SCENE RUNNING IN THE DIRECTOR ALREADY if(![director_ runningScene]){ [director_ setView:glView]; // SET THE DIRECTOR VIEW if( ! [director_ enableRetinaDisplay:YES] ) // ENABLE RETINA CCLOG(@"Retina Display Not supported"); [director_ runWithScene:[HelloWorldLayer scene]]; // RUN THE SCENE } else { // THERE IS A SCENE, START SINCE IT WAS STOPPED AND REPLACE TO RESTART [director_ startAnimation]; [director_ replaceScene:[HelloWorldLayer scene]]; } [director_ setDelegate:(id <CCDirectorDelegate>) [[UIApplication sharedApplication] delegate]]; // I DO NOT PUSH BECAUSE I ALREADY PUSHED TO THIS CONTROLLER, SO I ADD THE COCOS2D VIEW AS A SUBVIEW [self.view addSubview:[director_ view]]; } 

希望这段代码能帮助你,因为我花了整整一天的时间来解决这个问题。 这可能不是正确的方式,甚至是最漂亮的方式,但它的工作:)

编辑:另外,请不要,如果你POP的COCOS2D场景,你不必[[CCDirector sharedDirector] end] ,当视图dealloc /删除animation将被停止。

我花了好几天的时间寻找有关这方面的信息,并将与您分享我自己的经验。 我也试图创build一个加载到一个UITableViewController的游戏,当触摸一个单元格时,从这个游戏加载CCDirector。 这是一个游戏中心回合制游戏,因此devise(思考与朋友的话)。 到目前为止我发现的最好的方法如下(注意我在2.0中工作 – 其中CCDirector是UIViewController的子类):

在AppDelegate.h中,创build一个新的ivar来保存从模板代码创build的CCGLView。 然后将在didFinishLaunching中创build的CCGLView分配给您的新伊娃。 这允许导演重新使用最初创build的视图,而不是每次重新加载CCDirector时重新创build它,这似乎会导致我的经验中的各种奇怪的问题。

你也想在AppDelegate中创build一个名为-setupDirector的新方法,或者你可以在其中设置一个新的方法。 这应该在每次重新创buildCCDirector时调用。 我在下面发布了我的版本。 请注意,我的视觉CCGLView被称为“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_作为根视图控制器来设置导航控制器。 在这里,你要分配和初始化你的菜单视图控制器,并添加THAT而不是director_:

 // Create a Navigation Controller with the Director gamesTVC_ = [[GamesTableViewController alloc] initWithStyle:UITableViewStyleGrouped]; navController_ = [[UINavigationController alloc] initWithRootViewController:gamesTVC_]; navController_.navigationBarHidden = NO; 

didFinishLaunching中的其他所有内容都可以保持不变。 现在,在您的startGameButtonPressed方法的menuViewController中,您将调用您的应用程序实例上的新创build的setupDirector方法,该方法通过调用:

 AppController *app = (AppController *)[[UIApplication sharedApplication] delegate]; if ([CCDirector sharedDirector].runningScene) { [[CCDirectorIOS sharedDirector] end]; } [app setupDirector]; [app.navController pushViewController:app.director animated:YES]; 

我包括一个检查,以确保CCDirector还没有运行,如果是,结束它。 在你的游戏层,当你想popup视图控制器的时候,你会简单地调用它:

 AppController *app = (AppController *)[[UIApplication sharedApplication] delegate]; [app.navController popViewControllerAnimated:YES]; [[CCDirector sharedDirector] end]; 

这个stream程应该允许你自由地使用导航控制器来用CCDirector推动你的游戏场景,当你想返回到基于UIKit的主菜单时,popup视图控制器。 我希望这会有所帮助,因为我花了很多时间试图为自己的游戏做好准备。

最好的办法是在director中调用startAnimation和stopAnimation,但保留cocos2d视图并重新使用它。

任何试图closurescocos2d及其OpenGL视图并稍后重新初始化的尝试都会导致或多或less的问题,因为它实际上没有经过足够的testing。 除此之外,cocos2d在UIKit中工作得很好,它与其他OpenGL ES应用程序在与UIKit视图混合时具有相同的问题。

根据我的经验,科科斯并不是真的支持结束和恢复,它的行为就像已经完成,几乎closures了。

有两件事你可以尝试,第一个(我想到的是)在导演结束之后,又想再次开始之前调用CC_DIRECTOR_INIT (可能不是确切的名字)。

第二个是编辑导演源代码,并修改end方法,使其离开可用状态(停止释放视图和清除caching,等等)。 或者,您可以修改start方法,以确保Cocos在启动之前处于可用状态。

可悲的是,科科斯并没有让我们很容易地使用UIKit + Cocos,但运气不错。