Cocos2d 引擎 - 暂停,恢复

Cocos2d Engine - Pause , resume(Cocos2d 引擎 - 暂停,恢复)

本文介绍了Cocos2d 引擎 - 暂停,恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个游戏场景,它有 2 个图层,如下所示,当用户点击暂停按钮时,我将暂停窗口图层作为子图层添加到状态栏图层.游戏正在进行中,所以到目前为止,我实现的是将一个精灵加载到我的游戏层中,并将精灵移动到用户触摸的任何位置.

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.

就游戏层"上的触摸处理而言,一切正常,直到用户点击暂停按钮,问题是游戏层"触摸即使在调用 [[CCDirector sharedDirector] pause] 后仍处于活动状态.我仍然可以在暂停模式下在屏幕上移动我的播放器.

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.

请说明 Director pause 和 Touch 之间的关系是什么?

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

场景:游戏场景有 2 个孩子

- 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

图层:暂停 CCLayer

- PauseGameLayer
  - Resume Button   
     Tap 
     {
         Remove this layer from parent
         [[CCDirector sharedDirector] resume];
     }
  - Restart Level
  - Main menu

推荐答案

director 暂停时,touch dispatcher 并没有暂停,它仍然会派发所有的 touch 事件.我使用游戏状态(整数)实现了游戏暂停/恢复.

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;
   //.....
}

在许多其他情况下,使用游戏状态非常有用.在我的实现中,我在暂停游戏时从不暂停导演.暂停导演是暂停游戏最简单的方法,但是如果你需要在暂停层做一些动画(例如动物跳跃,文字闪烁......),你显然不能,因为导演被暂停了.解决方案是暂停所有游戏参与者和游戏层本身的调度程序和操作.pause 和 resume 方法应如下所示(假设您将所有游戏 Actor 存储在名为 allActors 的数组中)

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

希望这会有所帮助:)

这篇关于Cocos2d 引擎 - 暂停,恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Cocos2d 引擎 - 暂停,恢复