【问题标题】:Game Scene crashes when I restart the game重启游戏时游戏场景崩溃
【发布时间】:2012-12-12 10:32:49
【问题描述】:

我正在为 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

谁能告诉我这是什么原因。不胜感激。。谢谢。

【问题讨论】:

  • @LearnCocos2D - 你能帮我吗?
  • @Paras Joshi - 你能帮我解决这个问题吗?
  • 尝试在 startAnimation 之后运行 replaceScene 看看是否会有所不同。发布围绕 CCDirectorIOS.m:498 的代码行。在 Xcode 中启用全局异常断点,使其停在有问题的行。

标签: iphone ios cocos2d-iphone


【解决方案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]];

【讨论】:

    【解决方案2】:

    试试

    [[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];
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多