【问题标题】:Cocos2d implementation of Pause - The Game scene/layer issuePause的Cocos2d实现-游戏场景/图层问题
【发布时间】:2015-01-15 19:00:31
【问题描述】:

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

下面是代码:

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 部分时,暂停和恢复工作完美。但是当我实现其中任何一个时,在退出游戏时,replaceScene 无法以某种方式工作。我认为这是因为游戏进入暂停模式,但即使我使用调度程序调用 replaceScene 延迟 5-10 秒,它也无法正常工作。

如果我不使用这些stopAnimationpause,即使我的游戏暂停,一些被给予CCAction 的精灵也不会被暂停并继续。

感谢您的帮助,因为我使用的是 Cocos2d 3.0 并且找不到任何合适的帮助。

【问题讨论】:

  • start/stopAnimation 与暂停不同,它只会停止进行任何屏幕刷新(因此,如果在 cocos2d 视图上绘制了其他任何内容,您将获得视觉故障)。要暂停图层或场景,请将 paused 属性设置为 YES 或仅使用 CCDirector 的暂停/恢复方法。
  • @LearnCocos2D 我尝试使用 CCDirector 的暂停/恢复方法,但没有调用 replaceScene。如果我删除 stopAnimation/startAnimation,那么我也无法调用 replaceScene。我还尝试在另一个线程中延迟 1 秒后调用“ReloadGame”,考虑到“恢复”可能需要一些时间并且需要在其他块中执行逻辑。您可以看到注释的 'scheduleOnce' 方法,但没有输出。我不知道我是否在这里遗漏了一些基础知识,即使是任何好的链接也会受到赞赏。谢谢你的帮助。
  • @LearnCocos2D 你能给我这些方法的正确排序吗?
  • @LearnCocos2D 'paused' 属性工作正常,但它没有停止使用 CCActions 加载到 sprite 上的动作。我的精灵在暂停时继续运行 CCActions 事件:(。对此有什么建议吗?感谢 paused 属性。需要查看 Director 上的属性和方法之间的区别:)。
  • 您可能想通过代码示例提出一个单独的问题。一个真正暂停的节点真的应该暂停它的动作,所以要么这些动作没有在那个节点上运行,要么你可能正在做一些非常奇怪的事情(比如用 NSTimer 或 performSelector:afterDelay: 也许?)

标签: iphone cocos2d-iphone ccaction


【解决方案1】:

感谢@LearnCocos2D 的提示。

只需删除代码stopAnimationstartAnimationpauseresume 方法,我就能成功实现暂停/恢复。我只是用节点的paused 属性替换了这些。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多