3
votes

I have a spawn action and I was using NSTimer to create it, but I changed it to an SKAction so that it would stop when the scene is paused:

let SpawnAction = SKAction.sequence([SKAction.waitForDuration(0.5), SKAction.runBlock(Spawn)])
self.runAction(SKAction.repeatActionForever(SpawnAction))

Now the problem is the lag that exist on the waitForDuration part of the code I think, as it won't work right after the scene is unpaused.

This is the code I'm using to pause/unpause the game:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        let node = self.nodeAtPoint(location)
        if (node.name == "Pause") {
            PauseLabel.removeFromParent()
            self.addChild(ResumeLabel)
            self.runAction(SKAction.runBlock(self.pauseGame))
        }
        else if (node.name == "Resume") {
            self.view!.paused = false
            self.addChild(PauseLabel)
            ResumeLabel.removeFromParent()
        }
        else {
            //Other code...
        }
    }
}

func pauseGame() {
    self.view!.paused = true
}

I would like someone to tell me the way to properly pause/unpause a SpriteKit game, as all the methods I got off the internet didn't work properly for me. Thank you.

1
Thanks! I always thought self.view.paused was the method to use in SpriteKit, but self.paused seems to work a lot better. - Carlo Blasi

1 Answers

0
votes

i'm not sure if this will work for you. but under the didMoveToView function within the selected handler for the button i use to pause:

i used self.gameState = .Pause
as well as used self.physicsWorld.speed = 0

these both stop the game view and the physicsworld helps stop any of the skactions that may be running on your sprites.

hope this helps you.