gpt4 book ai didi

xcode - 为什么我的计时器在 Swift 中不倒计时到 0?

转载 作者:行者123 更新时间:2023-11-28 06:49:57 25 4
gpt4 key购买 nike

我有这个计时器,它从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com