为什么我的计时器在Swift中倒数到0?

时间:2016-02-04 03:36:05

标签: xcode swift timer nstimer

我有这个计时器,它从3开始,它应该倒数到0但它停在2.我不明白为什么它不会一直下​​降到0.你能告诉我什么是Im我的代码做错了。谢谢!

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()
    }

    }


} 

1 个答案:

答案 0 :(得分:1)

出现问题是因为在var之后显示使用后减少了它。将它移到前面并从4开始。

timerCountDownLabel.text = String(--timerToStartGame)