0

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

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

4

2 回答 2

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]];
于 2014-11-11T12:51:40.793 回答
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];

希望这可以帮助!

于 2012-12-13T09:36:18.363 回答