msqar msqar - 5 months ago 55
Swift Question

Pausing my game isn't working in Swift SpriteKit

Followed a few other posts like this one but for some reason, aren't working properly for my case.

Can anyone explain me why or what am I doing wrong?

So for now, I got a SKLabelNode that at is attached in the frame at the very beginning of my game, but with alpha = 0. That only says "PAUSED".
The idea is to show it by setting alpha = 1 when the pause button is pressed, and alpha = 0 when pressed again and everything goes back to normal.
I thought it would be a better idea than removing and attaching back again the same Sprite/Label over and over again. (If not, let me know)

Here's my code:

func showPauseModal() {
print("opening pause modal ", self.view!.paused)

if self.view!.paused {
self.pausedLabel.alpha = 0
self.unpauseGame()
}else{
self.pausedLabel.alpha = 1
self.pauseGame()
}
}


And then these are pauseGame() and unpauseGame() functions

func pauseGame() {
let delay = SKAction.waitForDuration(0.5)
let block = SKAction.runBlock({
self.view!.paused = true
})
let sequence = SKAction.sequence([delay, block])

self.runAction(sequence)
}

func unpauseGame() {
let delay = SKAction.waitForDuration(0.5)
let block = SKAction.runBlock({
self.view!.paused = false
})
let sequence = SKAction.sequence([delay, block])

self.runAction(sequence)
}


So, the first time i press the pause button in the screen, it pauses and adds the PAUSED label. When i press it again, the pause never goes away neither the label, although im checking it's getting inside the unpauseGame function. So what's wrong?

Thanks in advance.

Answer

I do not think runAction will be performed while the game is paused. Try simply

func unpauseGame() {
    self.view!.paused = false
}