Cocos2d引擎 - 暂停,恢复 [英] Cocos2d Engine - Pause , resume

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

问题描述

我有一个游戏场景,有两个层,如下所示,当用户点击暂停按钮,我添加一个暂停窗口层作为孩子到状态栏图层。游戏正在进行中,直到现在我已经实现了一个精灵加载到我的游戏层,并将精灵移动到用户触摸的位置。

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.

请澄清导演暂停和触摸之间的关系是什么?

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


推荐答案

导演暂停时,触摸调度程序未暂停,仍会分派所有触摸事件。我用游戏状态(一个整数)实现游戏暂停/恢复。

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

使用游戏状态在许多其他情况下非常有用。
在我的实现中,我暂停游戏时从不暂停主任。暂停导演是暂停游戏的最简单的方法,但如果你需要在暂停层中做一些动画(例如动物跳跃,文本闪烁...),你显然不能,因为导演暂停。解决方案是暂停所有游戏角色和游戏层本身的调度和动作。暂停和恢复方法应该看起来像(假设你存储所有游戏角色在一个数组命名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引擎 - 暂停,恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆