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.