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