2011-05-02 254 views
4

我有一个游戏场景,有两层,如下图所示,当用户点击暂停按钮时,我将一个暂停窗口层添加为状态栏层。游戏正在进行,所以到目前为止,我已经实现了将一个精灵加载到我的游戏层中,并将精灵移动到用户触摸的位置。Cocos2d引擎 - 暂停,恢复

就“游戏层”上的触摸处理而言,一切工作都很完美,直到用户点击暂停按钮,问题即使在调用[[CCDirector sharedDirector] pause]之后,“游戏层”触摸仍处于活动状态。我仍然可以在暂停模式下在屏幕上移动我的播放器。

请说明Director暂停和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 
+0

任何帮助,请让我知道如果我没有转达我的问题是否正确? – Anandh 2011-05-02 14:20:03

回答

3

当导演暂停,触摸调度程序没有被暂停,但它仍然会调度所有触摸事件。我用游戏状态(整数)实现了游戏暂停/恢复。

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游戏演员)

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

希望这有助于:)

+0

如果allActors是一个数组(NSArray等),那么调用resumeSchedulerAndActions将会崩溃,因为它不存在。你使用另一个集合吗? – Jonny 2013-04-03 08:12:26

0

创建一个新图层,当用户按下按钮 “暂停”,“暂停层“与按钮恢复,它可能会解决您的问题,但我不知道这是否是正确的解决方案。

也许这个链接可以帮助你Cocos2d pause

+0

我只是添加一个新图层,我的问题是我的游戏图层正在接收触摸。我遵循相同的教程,我有一个疑问,他用这两种方法? [self stopGameLoopTimer]; [self stopCollisionTimer]; – Anandh 2011-05-03 19:52:10

+1

重要的是要注意,暂停导演并不意味着所有事件都会影响到图层,因此考虑到这一点,他制作了两个方法'[self stopGameLoopTimer]'和'[self stopCollisionTimer]'。猜测'stopGameLoopTimer'将停止主要的游戏循环,他将更新像玩家精灵位置或其他事件的一些事件。 – 2011-05-03 21:19:51

0

您可以启用和禁用触摸这样的:

禁用触摸暂停前:

[self setIsTouchEnabled:NO] 

恢复后再次启用:

[self setIsTouchEnabled:YES] 

在cocos2d-x

this->setTouchEnabled(false);