为什么我的计时器在 Swift 中没有倒计时到 0?
Why doesnt my timer countdown to 0 in Swift?
我有这个计时器,它从 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)
我有这个计时器,它从 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)