0

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.

Community
  • 1
  • 1
msqar
  • 2,940
  • 5
  • 44
  • 90

1 Answers1

1

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

func unpauseGame() {
    self.view!.paused = false
}
Mr Flynn
  • 415
  • 2
  • 10
  • Ohh you were right! Thanks. I will put you as the solver. But i got another doubt. I got some physic bodies in my game, if i unpause the game just before pausing, like real fast, whenever i go back to the game, all the bodies are in the ground, instead of staying where they were before pausing. Now, if i wait for example 5 seconds, they will stay in the same place, do you know why? – msqar Jul 01 '16 at 19:49
  • Pausing the view does not pause the physics. You can pause the physics with scene.physicsWorld.speed = 0 – Mr Flynn Jul 01 '16 at 20:00
  • And then when i unpause the game everything will go back to normal? or do i have to set the speed again? – msqar Jul 01 '16 at 20:01
  • you have to set the speed again to 1 – Mr Flynn Jul 01 '16 at 20:03
  • Mmmm no, still happening. Doing this if self.view!.paused { self.pausedLabel.alpha = 0 self.pausedModalBackDrop.alpha = 0 self.physicsWorld.speed = 1 self.unpauseGame() }else{ self.pausedLabel.alpha = 1 self.pausedModalBackDrop.alpha = 0.5 self.physicsWorld.speed = 0 self.pauseGame() } – msqar Jul 01 '16 at 20:05
  • Hmm, I am going by apple documentation at [apple](https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsWorld_Ref/index.html#//apple_ref/occ/instp/SKPhysicsWorld/speed) try changing the speed of the physics world regardless of your pause mechanism to make sure your self.physicsworld is working correctly – Mr Flynn Jul 01 '16 at 20:14
  • Yea true, says that. But maybe this is being caused by something else... i can't figure out why, if it's my game or what. I mean, why is it that if i wait more than 3 seconds, everything is normal, and before, it lags and bodies goes down to floor. I will keep doing more research :D thanks tho – msqar Jul 01 '16 at 20:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116232/discussion-between-mr-flynn-and-msqar). – Mr Flynn Jul 01 '16 at 20:16