view?.ispaused = true with skscene not working

Viewed 370

so I'm working with a game in SpriteKit and I am using this code to pause the game.

self.pauseButton.alpha = 0
self.playButton.alpha = 1
self.settingsBackground.alpha = 0.85    
self.isPaused = true    
self.pauseButton.alpha = 1
self.playButton.alpha = 0
self.settingsBackground.alpha = 0

The code which is run before pausing is to change the appearance of the pause and the code afterward reverts it. The problem is that the code before pausing is not getting run and instead the game is just pausing before changing the visuals around. I've tried adding delays, making the steps SKActions, and testing just the code before the pause. When I run just the first 3 lines the visuals change properly but obviously, the game doesn't pause. When I run the whole thing the game pauses but the visuals don't change. Help!

1 Answers

the problem is that even when a scene isPaused the code still get run. Yes you are pausing the scene, yes the visuals do get run...but the visuals after the isPaused line also get run and reset the visuals that you just changed.

here is a really simple example of how this working. there are 3 boxes on the scene; the bottom box has an action that repeatedly scales up and down, and will stop when the scene is paused. the top box will pause the game when pressed and will reveal the middle box which will unpause the game.

class GameScene: SKScene {

    private var test: SKSpriteNode!
    private var test2: SKSpriteNode!
    private var test3: SKSpriteNode!

    override func didMove(to view: SKView) {

        backgroundColor = .clear

        test = SKSpriteNode(color: .blue, size: CGSize(width: 200, height: 200))
        test.position = CGPoint(x: 0, y: 350)
        addChild(test)

        test2 = SKSpriteNode(color: .red, size: CGSize(width: 200, height: 200))
        test2.position = CGPoint(x: 0, y: 0)
        test2.alpha = 0
        addChild(test2)

        test3 = SKSpriteNode(color: .yellow, size: CGSize(width: 200, height: 200))
        test3.position = CGPoint(x: 0, y: -350)
        addChild(test3)

        let scaleDown = SKAction.scale(to: 0.1, duration: 1.0)
        let scaleUp = SKAction.scale(to: 1.0, duration: 1.0)
        let sequence = SKAction.sequence([scaleDown, scaleUp])
        let repeater = SKAction.repeatForever(sequence)
        test3.run(repeater)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first as UITouch! {

            let touchLocation = touch.location(in: self)

            if test.contains(touchLocation) {
                print("pausing")
                self.test2.alpha = 1
                self.test.alpha = 0
                self.isPaused = true
            }

            if test2.contains(touchLocation) {
                print("unpausing")
                self.test2.alpha = 0
                self.test.alpha = 1
                self.isPaused = false
            }
        }
    }
}
Related