Spritekit - Keep the game paused when didBecomeActive

自作多情 提交于 2019-11-28 06:33:31

问题


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 ?


回答1:


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;
}

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()
    {
    }
}


来源:https://stackoverflow.com/questions/33179409/spritekit-keep-the-game-paused-when-didbecomeactive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!