重启游戏时游戏场景崩溃

时间:2012-12-12 10:32:50

标签: iphone ios cocos2d-iphone

我正在为IOS6使用cocos2D游戏引擎开发游戏。我使用以下代码首次启动游戏:

-(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

有谁可以告诉我这个的原因。非常感谢..谢谢。

2 个答案:

答案 0 :(得分:1)

当您可以使用CCScene类的场景方法时,该类将重新初始化并再次构建整个场景。因此,如果要用相同的场景替换场景,则不必再次启动动画,也可以隐藏glView,您可以尝试暂停导演并在取消隐藏glView之前再次恢复它。

[[CCDirector shareDirector]pause];
[[CCDirector sharedDirector].openGLView setHidden:YES];

[[CCDirector shareDirector]resume];
[[CCDirector sharedDirector].openGLView setHidden:YES];
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];

答案 1 :(得分:0)

尝试

[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] pause];

[[CCDirector sharedDirector] stopAnimation]; // call this to make sure you don't start a second display link
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];

希望这有帮助!