Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 为什么我的计时器在Swift中不倒计时到0?_Xcode_Swift_Timer_Nstimer - Fatal编程技术网

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

Xcode 为什么我的计时器在Swift中不倒计时到0?,xcode,swift,timer,nstimer,Xcode,Swift,Timer,Nstimer,我有一个计时器,它从3开始,应该倒计时到0,但它在2停止。我不明白他们为什么不降到0。你能告诉我我的代码有什么问题吗。谢谢大家! class GameScene: SKScene, SKPhysicsContactDelegate { var timerToStartGame = 3 var timerCountDownLabel: SKLabelNode! = SKLabelNode() override func didMoveToView(view: SKView) { timer

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

它仍然在2停止。好的,它可以工作,但是当它达到2时,func被调用。我需要在计时器达到0时调用它。当达到2时,如果条件==0My bad,则无法调用它。当计时器达到1时,将调用它。