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

时间:2021-10-09 02:47:27

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!

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

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 个解决方案

#1


1  

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

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

timerCountDownLabel.text = String(--timerToStartGame)

#1


1  

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

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

timerCountDownLabel.text = String(--timerToStartGame)