Pausing a SpriteKit Game - UIApplicationWillResignActive vs. UIApplicationDidBecomeActive?

荒凉一梦 提交于 2019-12-10 10:46:51

问题


I understand the differences between the two methods but I was wondering if there were disadvantages to using UIApplicationWillResignActive as opposed to UIApplicationDidBecomeActive (or, UIApplicationDidEnterBackground) when doing something such as pause a SpriteKit game. I've been roaming past threads on the site and have noticed that generally in response to questions asking how to pause a SpriteKit game, UIApplicationDidBecomeActive and UIApplicationDidEnterBackground are suggested as opposed to UIApplicationWillResignActive. Since UIApplicationWillResignActive also pauses the games in response to Siri/Calls, wouldn't this be the better method to use when dealing with pausing SpriteKit games and such or is there some disadvantage to using it?

Below I have my Notifications and pause function:

NotificationCenter.default.addObserver(self, selector: #selector(self.pauseGame), name:NSNotification.Name.UIApplicationDidBecomeActive, object: nil)

//OR

NotificationCenter.default.addObserver(self, selector: #selector(self.pauseGame), name:NSNotification.Name.UIApplicationWillResignActive, object: nil)

Pause function:

func pauseGame(){
    print(self.scene?.isPaused)
    if sceneIsPaused == true || isGameOver == true{
        return
    }

    leftMobGenTimer.invalidate()
    rightMobGenTimer.invalidate()
    genDragonTimer.invalidate()
    checkNinjaTimer.invalidate()

    chiStrikeTimer.invalidate()
    randCoinTimer.invalidate()

    print("Timer stopped?")

    if soundEnabled == true{
     player.pause()
    }

    self.scene?.isPaused = true
    sceneIsPaused = true
    makePauseView()

}

回答1:


Pausing

This is from the docs about applicationWillResignActive method:

You should use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. An app in the inactive state should do minimal work while it waits to transition to either the active or background state.

So this method is meant to be used when it comes to pausing the games.

Unpausing

iirc, game should be automatically unpaused when app transition from inactive to active state. This is the moment when applicationDidBecomeActive method is called, so in some cases you may want to pause the game here as well... But that depends on your game.

I don't know why did you mention combination of applicationDidBecomeActive & applicationDidEnterBackground for pausing/unpausing... Because it is probably too late to pause the game in applicationDidEnterBackground and technically it is not needed to unpause the game in applicationDidBecomeActive because this is done automatically.



来源:https://stackoverflow.com/questions/44854589/pausing-a-spritekit-game-uiapplicationwillresignactive-vs-uiapplicationdidbec

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