0

I have the following code in my AppDelegate:

func applicationDidEnterBackground(_ application: UIApplication) {
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "PauseGame"), object: self)
}

In my GameScene.swift file, I have the the observer:

NotificationCenter.default.addObserver(self, selector: #selector(GameScene_split.pauseGame), name: NSNotification.Name(rawValue: "PauseGame"), object: nil)

Here is the function that it calls:

@objc func pauseGame() {
    self.isPaused = true
    isGamePaused = true
    pauseButton.texture = SKTexture(imageNamed: "PlayButtonWhite")
}

Okay, so when the home button is pressed and the app goes into the background, this pauseGame function is indeed called because when I click back into the app, the pauseButton texture is has changed to the "PlayButtonWhite", but the game is not paused.

Any ideas on how to stop the game from automatically unpausing when the app becomes active?

c0nman
  • 189
  • 1
  • 12
  • This may be what you're looking for https://stackoverflow.com/questions/26317553/pausing-spritekit-game-on-app-launch-exit-ios8 – 0x141E Aug 18 '18 at 09:15

1 Answers1

0

When the game is resumed the scene is (somehow) automatically unpaused.

However you can check your property isGamePaused and pause the game again.

Here's the code for your Scene

override func update(_ currentTime: TimeInterval) {

    guard !isGamePaused else {
        self.view?.isPaused = true
        return
    }
}

Let me know if it works.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148