2012-12-12 226 views
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 

誰能告訴我這樣做的原因。這將不勝感激..謝謝。

+0

@ LearnCocos2D - u能幫助我在這? – NiKKi

+0

@Paras Joshi - 你能幫助我嗎? – NiKKi

+0

嘗試在startAnimation之後運行replaceScene以查看是否有所作爲。張貼圍繞CCDirectorIOS.m的代碼行:498。在Xcode中啓用全局異常斷點,使其停在違規行。 – LearnCocos2D

回答

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

希望這有助於!

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