Cocos2d实现暂停 - 游戏场景/图层问题

时间:2015-01-15 19:00:32

标签: iphone cocos2d-iphone ccaction

我正在尝试为我的游戏创建暂停屏幕。游戏是在连续滚动背景(从上到下)开发的,障碍物独立移动(从左到右)。

以下是代码:

  

PauseScene.h

@interface PauseScene : CCNode {}
@property (nonatomic, retain) HelloWorldScene *mainScene;
//+ (PauseScene *)scene;
- (id) init;
@end
  

PauseScene.m

@implementation PauseScene

- (id) init {
    if (self = [super init]) {
        // Adding RESUME and QUIT button and few others to make the whole Pause screen
    }
    return self;
}

// Called the method when Resume button is selected
- (void) Resume:(id) sender {
    //[[CCDirector sharedDirector] startAnimation];
    //[[CCDirector sharedDirector] resume];

    self.mainScene.userInteractionEnabled = YES;
    [self.mainScene removeChild: self];
    [self.mainScene StartGame]; // StartGame in MainScene contains the same code which commented out above, I will paste StartGame method later
}

// Quit method is called when the Quit button is pressed
- (void) Quit:(id) sender {
    //[[CCDirector sharedDirector] resume];
    //[[CCDirector sharedDirector] startAnimation];

    self.mainScene.userInteractionEnabled = YES;
    [self.mainScene removeChild: self];
    NSLog(@"Pre Reload", nil);
    //[self.mainScene scheduleOnce:@selector(ReloadGame) delay: 1.0f];
    [self.mainScene ReloadGame]; // Reload method contains the code commented above
}

@end

PauseScene仅仅是节点类。

  

MainScene文件中的StartGame和ReloadGame方法

- (void) StartGame {
    // Create and display HUD layer, like, scores, navigation buttons, etc.

    // Resuming the game
    //[[CCDirector sharedDirector] stopAnimation];
    [[CCDirector sharedDirector] resume];
    [[CCDirector sharedDirector] startAnimation];
}

- (void) ReloadGame {
    // Resume the game
    //[[CCDirector sharedDirector] stopAnimation];
    [[CCDirector sharedDirector] resume];
    [[CCDirector sharedDirector] startAnimation];

    [[CCDirector sharedDirector] replaceScene:[HelloWorldScene scene] withTransition:[CCTransition transitionFadeWithDuration:1.0f]];
}
  

暂停按钮显示在MainScene的HUD层中,因此,当点击暂停按钮时,我们将暂停游戏并显示PauseScene

- (void)onPauseClicked:(id)sender {
    // Code to display the pause node of this scene

    // Pause the game
    [[CCDirector sharedDirector] stopAnimation];
    [[CCDirector sharedDirector] pause];
}

当我评论stopAnimationpause部分时,Pause and Resume完美无缺。但是当我实现其中任何一个时,在quitGame时,replaceScene无法以某种方式工作。我认为这是因为游戏进入暂停模式,但即使我使用调度程序在5-10秒的延迟时调用replaceScene,它也无法正常工作。

如果我不使用这些stopAnimationpause,那么给定CCAction的某些精灵不会暂停,即使我的游戏暂停也会继续。< / p>

感谢您的帮助,因为我使用的是Cocos2d 3.0,无法找到任何适当的帮助。

1 个答案:

答案 0 :(得分:0)

感谢@ LearnCocos2D提供的建议。

我只需删除代码stopAnimationstartAnimation以及pauseresume方法,即可成功实施暂停/恢复。我只是用节点的paused属性替换了这些。

要暂停游戏,需要为游戏中的每个节点设置暂停= YES,然后反向恢复。