Cocos2d Engine - Pause , resume

后端 未结 3 1019
再見小時候
再見小時候 2021-02-10 18:51

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

相关标签:
3条回答
  • 2021-02-10 19:06

    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 讨论(0)
  • 2021-02-10 19:08

    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);
    
    0 讨论(0)
  • 2021-02-10 19:19

    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 讨论(0)
提交回复
热议问题