NiKKi
NiKKi

Reputation: 3296

Game Scene crashes when I restart the game

I am developing a game using cocos2D game engine for IOS6. I use the following code for starting the game for the first time :

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

When i go out of the game scene i do not end the cocos2D game engine, instead i just stop the animations and hide the glView. I use this code.

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

When i again start the game to be played, I use this code :

[[CCDirector sharedDirector].openGLView setHidden:NO];
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];
[[CCDirector sharedDirector] startAnimation];

It works fine.

But when I start the game for the first time, and the i go back from the game scene, and then I exit the application by pressing the home button of device, then I again start the application, and then I restart the game, I get a crash in this scenario.

The console prints :

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

Can Anyone tell me the reason for this. It would be appreciated.. Thanks.

Upvotes: 0

Views: 400

Answers (2)

Markandaiya
Markandaiya

Reputation: 9

When you can the scene method of a CCScene class, the class gets reinitialised and the whole scene is constructed again. therefore, you do not have to start the animations again if you are replacing the scene with the same scene, also it you are hiding the glView you can try pausing the director and resume it again before you unhide the glView.

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

and

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

Upvotes: 1

hjm
hjm

Reputation: 421

Try

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

and

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

Hope this helps!

Upvotes: 0

Related Questions