【发布时间】:2026-02-08 09:10:01
【问题描述】:
我已经阅读了我能找到的关于这个主题的所有内容,但仍然无法弄清楚我的问题。我尝试在 appdelegate 的每个区域暂停我的游戏
func applicationWillResignActive(application: UIApplication!) {
NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}
func applicationDidEnterBackground(application: UIApplication!) {
NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}
func applicationWillEnterForeground(application: UIApplication!) {
NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}
func applicationDidBecomeActive(application: UIApplication!) {
NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}
在我的控制器中:
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseGame:", name: "pauseGameScene", object: nil)
}
func pauseGame(){
self.skView.paused = true
self.skView.scene!.paused = true
}
我知道 pauseGame 有效,因为如果我在场景中使用按钮切换它,它将停止游戏。即使我在将它们加载到控制器后直接暂停我的 skview 和场景..游戏在启动时也不会暂停。当我在游戏中时,很容易暂停游戏。但出于某种原因,每当我退出并恢复应用程序时,游戏都会自行取消暂停。
我注意到如果我变得 hacky 并使用某种延迟.. 我可以让它工作。但显然这很愚蠢..我只需要知道游戏在哪里取消暂停!
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
func pauseGame(sender: UIButton!){
delay(2) {
println("blah")
self.skView.paused = true
self.skView.scene!.paused = true
}
}
【问题讨论】:
-
看起来精灵套件在应用程序进入后台模式之前自动暂停视图和场景。它还会在 willEnterForeground/didBecomeActive 被调用后取消暂停视图/场景。
-
是的,这正是我注意到的。有什么办法可以防止这种行为?
-
我怀疑这是精灵套件中的一个错误,因为在调用 didBecomeActive 后游戏会恢复。
标签: ios swift ios8 sprite-kit nsnotificationcenter