我正在使用 SpriteKit 开发一款游戏,该游戏可以在执行期间暂停并可以恢复。
但是当游戏暂停时按下主页按钮时,我遇到了 applicationDidEnterBackground
问题,因为当我恢复游戏时,即使游戏之前已暂停,实体也会立即开始移动。
我找不到在 AppDelegate
中实现 applicationDidEnterBackground
和其他相关方法的方法,因为它和我的 GameScene.swift
我实际上是用代码暂停实体
entitiesLayerNode.paused = true
gameState = .GamePaused
编辑:
我想显式暂停 entitiesLayerNode
因为我想保留其他“移动”节点。问题是,按照下面给出的建议,该方法会暂停一切!我只需要暂停该层。
我认为问题是我无法从 View Controller 访问 entitiesLayerNode
。
我通常使用代码片段
let mainScene = scene as GameScene
mainScene.entitiesLayerNode.pause = true
但是 Xcode 给我一个错误,指出 scene
是一个无法解析的标识符。
最佳答案
与 here 几乎相同
应用程序委托(delegate):
func applicationWillResignActive(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene", object: self)
}
游戏场景 View Controller :
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseGameScene"), name: "PauseGameScene", object: nil)
}
func pauseGameScene() {
if self.skView.scene != nil {
self.skView.paused = self.skView.scene.paused = true
}
}
关于ios - 游戏在快速从后台恢复后退出暂停状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27655162/