0

I have a game with animation. In game scene, I use the method below to pause my game (it works perfectly, because the game is paused when the users taps on any point of the iOS device):

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    CGPoint locationOfTouch=[self convertTouchToNodeSpace: touch];

    CGRect rectOfScreen=CGRectMake(0, 0, screenSize.width, screenSize.height);

if (CGRectContainsPoint(rectOfScreen, locationOfTouch)) 
{
    [[CCDirector sharedDirector] replaceScene:[PauseScene scene]];
    [self unschedule:@selector(spawnEnemies:)];
    [self unschedule:@selector(checkCollisionOfEnemyWithBullet:)];
    [self unschedule:@selector(update:)];
    [self unschedule:@selector(isJoystickActivated:)];
    [self unschedule:@selector(checkHasGameEnded:)];

    [[CCDirector sharedDirector] pause];
}

}

To implement resume action, I'v created PauseLayer:CCLayer, where I've implemented the following method to resume the game:

-(void) continueGame:(id)sender
{
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] replaceScene:[GameSceneLayer scene]];
//[self resumeSchedulerAndActions];
}

Here is the code, how I envoke the method above:

 CCMenuItemFont* continue_game=[CCMenuItemFont itemWithString:@"continue game" target:self selector:@selector(continueGame:)];

But, when I select continue game, the game start from the blank point: every game state, every game character will be new. How can I resume my game from the intial point where it was paused by user? Thank you!

mr.M
  • 851
  • 6
  • 23
  • 41

1 Answers1

0

You can use these two cocos2D function to pause and resume:

[self pauseSchedulerAndActions]; //pause
[self resumeSchedulerAndActions];//resume

Also maintain one bool variable, and check that whenever u need.

   bool mISGamePaused;
Guru
  • 21,652
  • 10
  • 63
  • 102
  • I am confused about using the bool variable mIsGamePaused. Why do I actually need this variable if I can only use these functions ? – mr.M Aug 19 '12 at 02:30
  • @TarasMurzenkov, In my game, I used many separate class for different obstacles. So with this bool variable we can avoid execution of some code if game s paused... – Guru Aug 19 '12 at 04:52
  • Could you post some code example? PS I've used the method [self resumeSchedulerAndActions] in -(void)continueGame:(id)sender, but my game didn't resume from pause state. – mr.M Aug 19 '12 at 08:32
  • I used same call, remove that replaceScene api in ur continueGame function. Also do not unschedule selector on pause...just check if game paused in those shedular..and if game paused then do not allow execution of function.. – Guru Aug 19 '12 at 08:54