4
votes

I have a game scene which has 2 layers as shown below , when the user taps the Pause button am adding a Pause window layer as a child to Status bar layer. Game is in progress so till now what I have implemented was loaded a sprite into my Game layer and moving the sprite to the location wherever the user touches.

As far as touch handling on "game layer" goes, everything works perfect until the user taps pause button , the problem am getting is "Game layer" touch is active even after calling [[CCDirector sharedDirector] pause]. Am still able to move my player around the screen in pause mode.

Please clarify what is the relation between Director pause and Touch?

Scene : Game scene has 2 Child

- GameScene
   - Status Bar Layer #1
     - Pause Button
        Tap 
        {
           [[CCDirector sharedDirector] pause]
           Add pause Window to Status bar layer;
        } 
     - Score Label
     - Life Status icon

   - Game Layer #2

Layer : Pause CCLayer

- PauseGameLayer
  - Resume Button   
     Tap 
     {
         Remove this layer from parent
         [[CCDirector sharedDirector] resume];
     }
  - Restart Level
  - Main menu
3
Any help , please let me know if I haven't conveyed my question properly?Sabha B

3 Answers

3
votes

When the director is paused, the touch dispatcher is not paused, it still dispatches all touch events. I implemented game pause/resume with a game state (an integer).

int _state;

- (void) pause{
   if(_state == kGameStatePlaying){
      _state = kGameStatePaused;
   //TODO - pause the game
   }
}

- (void) resume{
   if(_state == kGameStatePaused){
      _state = kGameStatePlaying;
   //TODO - resume the game
   }
}

- (void) ccTouchesBegan:(NSSet *) tiuches withEvent:(UIEvent *) event{
   if(_state != kGameStatePlaying) 
      return;
   //.....
}

Use a game state is very useful in many other cases. In my implementation, I never pause the director when pause the game. Pausing director is the easiest way to pause the game, but if you need to do some animations in the pause layer (eg. animal jumps, text blinks ...), you obviously cannot because the director is paused. The solution is to pause schedulers and actions of all the game actors and the game layer itself. The pause and resume methods should look like (assume that you store all game actor in an array named allActors)

- (void) pause{
   if(_state == kGameStatePlaying){
      _state = kGameStatePaused;
      [self pauseSchedulerAndActions];
      [allActors performSelector:@selector(pauseSchedulerAndActions)];
   }
}

- (void) resume{
   if(_state == kGameStatePaused){
      _state = kGameStatePlaying;
      [self resumeSchedulerAndActions];
      [allActors performSelector:@selector(resumeSchedulerAndActions)];
   }
}

Hope this helps :)

0
votes

Add a new layer when the user press the button "pause", a "pause layer" with a button resume, it may resolve your problem but i don't know if it's the right solution.

Maybe this link may help you Cocos2d pause

0
votes

You can enable and disable touch like this :

Disable touch before pause :

[self setIsTouchEnabled:NO] 

Enable again after resume :

[self setIsTouchEnabled:YES]

In cocos2d-x

this->setTouchEnabled(false);