问题
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