我有这个计时器,它从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()
}
}
}
最佳答案
出现此问题是因为在显示变量后,使用--after将其减少。将其移到前面并从4开始。
timerCountDownLabel.text = String(--timerToStartGame)
关于xcode - 为什么我的计时器在Swift中没有倒数为0?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35192032/