I think its not ideal to pause the whole scene, its better to have a worldNode and pause that node. This will also make your life easier for overlaying Menu nodes etc.
Apple also does this in their sample game DemoBots.
Create a world node in your scene and a isGamePause property
var isGamePaused = false
let worldNode = SKNode()
and add it in didMoveToView
addChild(worldNode)
Than add all your sprites to that node
worldNode.addChild(someSprite1)
worldNode.addChild(someSprite2)
Than in your pause function you say
func pauseTheGame() {
isGamePaused = true
worldNode.paused = true
physicsWorld.speed = 0
/// show pause menu
}
Your resume function should say
func resumeTheGame() {
isGamePaused = false
worldNode.paused = false
physicsWorld.speed = 1
// remove pause menu
}
To make extra sure that your game does not resume when paused I add a check in the update method to keep the game paused.
override func update(_ currentTime: TimeInterval) {
guard !isGamePaused else {
worldNode.paused = true
physicsWorld.speed = 0
return
}
...
}
As a tip you should always organise string keys into properties to avoid typos e.g Notification centre names, UserDefaults keys, SKAction keys etc.
With Swift 3 for Notification Center names you can now create an extension and handle them in a very neat way.
extension NSNotification.Name {
static let goToBackground = Notification.Name(rawValue: "goToBackground")
}
Now you can say
NotificationCenter.default.post(name: .goToBackground, object: self)
NotificationCenter.default.addObserver(self, selector: #selector(pauseTheGame), name: .goToBackground, object: nil)
Hope this helps