我正在使用cocos2D游戏引擎为IOS6开发一个游戏。我第一次启动游戏时使用以下代码:
-(void)addGameScene
{
CCDirector *director = [CCDirector sharedDirector];
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeMainLoop];
else {
[CCDirector setDirectorType:kCCDirectorTypeDisplayLink];
}
// Init the View Controller
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
if(!glView)
glView = [[EAGLView alloc] initWithFrame:[window bounds]];
[director setOpenGLView:glView];
[director setOpenGLView:glView];
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
[viewController setView:glView];
[window setRootViewController:viewController];
[window addSubview: viewController.view];
[window makeKeyAndVisible];
[[CCDirector sharedDirector] runWithScene: [HelloWorldLayer scene]];
}
当我退出游戏场景时,我不会结束cocos2D游戏引擎,而是停止动画并隐藏glView。我使用这个代码。
[[CCDirector sharedDirector] stopAnimation];
CATransition *animation3 = [CATransition animation];
[animation3 setDuration:0.5f];
[animation3 setType:kCATransitionFade];
[animation3 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[[CCDirector sharedDirector].openGLView layer] addAnimation:animation3 forKey:@"SwitchToView"];
[[CCDirector sharedDirector].openGLView setHidden:YES];
当我再次开始要玩的游戏时,我使用以下代码:
[[CCDirector sharedDirector].openGLView setHidden:NO];
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];
[[CCDirector sharedDirector] startAnimation];
它工作正常。
但是当我第一次启动游戏时,我从游戏场景返回,然后我通过按设备的主页按钮退出应用程序,然后我再次启动应用程序,然后重新启动应用程序游戏,我在这种情况下崩溃了。
控制台打印:
2012-12-12 15:53:24.847 CasinoApp[2856:12203] -[HelloWorldLayer init] : Screen width 480.00 screen height 320.00
2012-12-12 15:53:24.848 CasinoApp[2856:12203] 10.000000
2012-12-12 15:53:24.849 CasinoApp[2856:12203] -[CCTexture2D(Image) initWithImage:resolutionType:] : cocos2d: CCTexture2D. Can't create Texture. UIImage is nil
2012-12-12 15:53:24.850 CasinoApp[2856:12203] -[CCTextureCache addImage:] : cocos2d: Couldn't add image:lines in CCTextureCache
2012-12-12 15:53:24.850 CasinoApp[2856:12203] *** Assertion failure in -[CCDirectorTimer startAnimation], /Users/rakesh/Desktop/Ryan/Code/CasinoApp/CasinoApp/libs/cocos2d/Platforms/iOS/CCDirectorIOS.m:498
谁能告诉我这是什么原因?我们将不胜感激..谢谢。
最佳答案
当您可以使用 CCScene 类的 scene 方法时,该类将重新初始化,并再次构造整个场景。因此,如果您用相同的场景替换场景,则不必再次启动动画,而且如果您隐藏了 glView,您可以尝试暂停导演并在取消隐藏 glView 之前再次恢复它。
[[CCDirector shareDirector]pause];
[[CCDirector sharedDirector].openGLView setHidden:YES];
和
[[CCDirector shareDirector]resume];
[[CCDirector sharedDirector].openGLView setHidden:YES];
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];
关于iphone - 重新启动游戏时游戏场景崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13837758/