如何从 SKView 或 ViewController 暂停 SKScene

How to pause SKScene from SKView or ViewController

我有以下代码来暂停游戏场景:

class GameProcessScene: SKScene {
...
var onPause: Bool = false {
    willSet {
        self.paused = newValue
        self.view?.paused = newValue
    }
}
...
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch in touches {
        let location = touch.locationInNode(self)
        let possiblePauseNode = self.nodeAtPoint(location)
        if (possiblePauseNode.name == "pauseButton") {
            if (self.paused) {
                onPause = false
            } else {
                onPause = true
            }
            return
        }
    ...
    }
}

按下按钮暂停。但我也想在游戏从后台出现时暂停游戏。我正在尝试从 ViewController class:

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseGameScene", name: UIApplicationWillEnterForegroundNotification, object: nil)
}
func pauseGameScene() {
    let view = self.view as SKView
    let scene = view.scene
    switch scene {
    case is MainMenuScene:
        println("MainMenu")
    case let game as GameProcessScene:
        game.onPause = true
    default:
        println("default")
    }
}
deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

但是当游戏从后台出现时它不起作用。 有人可以帮忙吗?谢谢!

UPD1:也尝试了 UIApplicationWillResignActiveNotificationUIApplicationDidEnterBackgroundNotification,但 game.onPause = true 仍然无效。它把"SKScene.paused"属性改为true,但是当游戏进入前台时动作仍在执行,甚至游戏场景在进入后台之前暂停。

UPD2:我将观察者和选择器从 ViewController class 移动到 GameProcessScene: SKScene class - 效果相同,"paused" 属性 发生变化,但在事实上游戏还在继续。

您是否尝试将代码放入

override func viewWillAppear(animated: Bool) {
    //Put your pauseGameScene(); code here
} 

此方法每次都在您的视图出现之前调用。 viewDidLoad() 运行 在您的实例初始化后仅执行一次。

我通过创建一个名为 isPaused 的 属性 并覆盖 paused 属性 以始终遵循 isPaused 属性 解决了我的问题. 注意:这会禁止 SKSCene 在进入后台模式时自动暂停。您必须手动设置 isPaused 属性。所以只在你真正需要恢复暂停模式的场景中使用它。

class GameScene: SKScene,SKPhysicsContactDelegate {

       // Initializers

    var isPaused : Bool = false {
        didSet {
            self.paused = isPaused
        }
    }
     override var paused : Bool {
        get {
            return isPaused
        }
        set {
            super.paused = isPaused
        }
    }
}

在ViewController

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseGameScene", name: UIApplicationWillResignActiveNotification, object: nil)
}

func pauseGameScene() {
    println("will become inactive")
    let view = self.view as SKView
    let scene = view.scene
    switch scene {
    case let game as GameScene:
        game.isPaused = true
    default:
        println("default")
    }
}