Paul
Paul

Reputation: 449

SpriteKit pause Scene (Swift4.2)

I built a Game with a pause button which shows a new modal ViewController with a Pause menu over the current and pauses the scene with the following function (which is located in the scene and called via protocol and delegate):

func pause(isPaused: Bool) {
    let pauseAction = SKAction.run {
        self.view?.isPaused = isPaused
    }
    self.run(pauseAction)
}

Pausing the scene and showing the modal view controller works, but when I return to the scene by a unwind segue the scene doesn't unpause.

The pause button is in the same view as the scene. For communication with the scene from the Pause menu, I use an unwind segue where I call the pause function via Protocol and delegate.

I present the pause menu with that:

@IBAction func PauseMenuButton(_ sender: Any) {
    let vc = storyboard!.instantiateViewController(withIdentifier: "PauseMenuVC")

    vc.providesPresentationContextTransitionStyle = true
    vc.definesPresentationContext = true
    vc.modalPresentationStyle = .overCurrentContext
    present(vc, animated: true, completion: nil)

    SceneIsPausedDelegate.pause(isPaused: true)
}

and go back to the scene with that :

@IBAction func unwindToGameVC(segue: UIStoryboardSegue) {
    SceneIsPausedDelegate.pause(isPaused: false)
}  

I think that the function is never called because it is in the scene since it is paused the is no execution.

Upvotes: 0

Views: 362

Answers (2)

Paul
Paul

Reputation: 449

I changed:

func pause(isPaused: Bool) {
    let pauseAction = SKAction.run {
    self.view?.isPaused = isPaused
}
    self.run(pauseAction)
}

to:

func pause(isPaused: Bool) {
    self.view?.isPaused = isPaused
}

it wokes without an action.

Upvotes: 1

Knight0fDragon
Knight0fDragon

Reputation: 16827

SKView pause and SKScene pause behave differently

When an SKView is paused, it will not call the update functions on the scene

When an SKScene is paused, SKView will still call the update functions on the scene, and the scene will not call the update functions on the children nodes.

In your scenario, you are calling pause on the SKView. This means any action you run on your scene will not fire because update never happens.

I recommend either switching to SKScene pause, or not using an action to pause and unpause.

Upvotes: 1

Related Questions