Spritekit - 在 didBecomeActive 时保持游戏暂停 [英] Spritekit - Keep the game paused when didBecomeActive
问题描述
我有一个带有一个功能和一个按钮的暂停系统,它运行良好,我知道当应用程序进入后台时它会自动暂停,当它回来时它会自动取消暂停,我的问题是我不知道如何当它再次变为活动状态时保持暂停.
I have a pause system with a function and a button and it works perfect, i know when the app enters background it automatically pauses and when it comes back it automatically unpauses itself, my problem is that I don't know how to keep it paused when it becomes active again.
func applicationWillResignActive(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("Pause", object: nil) // tried here
}
func applicationDidBecomeActive(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("Pause", object: nil) // tried here
}
我分别尝试了这两种方法,但游戏继续运行,有时它会显示我的暂停菜单(游戏上方的一层)并且仍然在后台运行.那么实现这一目标的正确方法是什么?
I tried on both those methods separately but the game keeps running, sometimes it shows my Pause menu (a layer above the gameplay) and still run in the background. So what is the correct way to achieve this ?
推荐答案
在您的场景或视图中,您应该能够通过添加观察者来处理暂停
In your scene or view, you should be able to handle pause by adding an observer to it
NSNotificationCenter.defaultCenter().addObserver(self,selector:Selector("pauseGame:",name:"Pause",object:nil)
然后你添加一个函数来处理这个
Then you add a function to handle this
func pauseGame(notification:NSNotification)
{
self.paused = true;
}
现在请记住,我发现在 iOS 8 中存在一个错误,即 CBApplicationDidBecomeActive 会导致不良结果,因此您需要在 SKView 的类中重写它,如下所示:
Now keep in mind I have found that in iOS 8 there is a bug where CBApplicationDidBecomeActive can cause undesirable results, so you need to override this in your SKView's class like this:
class GameSceneView : SKView
{
...//Other Code
func CBApplicationDidBecomeActive()
{
}
}
这篇关于Spritekit - 在 didBecomeActive 时保持游戏暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!