coding22
coding22

Reputation: 689

Why doesnt my timer countdown to 0 in Swift?

I have this timer and it starts at 3 and it should countdown to 0 but it stops at 2. I don't get why it doesn't go all they way down to 0. Can you please let me know what Im doing wrong with my code. Thank you!

class GameScene: SKScene, SKPhysicsContactDelegate {

var timerToStartGame = 3
var timerCountDownLabel: SKLabelNode! = SKLabelNode()


override func didMoveToView(view: SKView) {

timerCountDownLabel = SKLabelNode(fontNamed: "TimeBurner")
timerCountDownLabel.fontColor = UIColor.whiteColor()
timerCountDownLabel.zPosition = 40
timerCountDownLabel.fontSize = 60
timerCountDownLabel.position = CGPointMake(self.size.width / 2.4, self.size.height / 1.5)
self.addChild(timerCountDownLabel)



var clock = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("countdown"), userInfo: nil, repeats: true)

}


func countdown() {
    timerCountDownLabel.text = String(timerToStartGame--)
    if timerToStartGame == 0 {
        doAction()
    }

    }


} 

Upvotes: 1

Views: 96

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236275

The problem occurs because you decrease it after displaying using -- after the var. Move it to the front and start from 4.

timerCountDownLabel.text = String(--timerToStartGame)

Upvotes: 1

Related Questions