所以我环顾四周,我无法找到我做错的事情,我正在运行Swift 1.2,顺便使用SpriteKit。
//showing you just in case
import SpriteKit
//My variables
var countDownLabel = SKLabelNode(fontNamed: "AmericanTypewriter-Light")
var time = 4
var timer = NSTimer()
//Don't want to give you my entire project, I know I can't override a function outside of its class
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if playButton.containsPoint(location) {
//I want this to run 4 times, but is only running once
func countdown() {
time--
countDownLabel.text = "\(time)"
}
//This is set to run my function every second for ever.
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector(countdown()), userInfo: nil, repeats: true)
playButton.removeFromParent()
countDownLabel.fontColor = SKColor.blackColor()
countDownLabel.fontSize = 70
self.addChild(countDownLabel)
}
}
我希望我的函数倒计时运行4次,但它只运行一次由于某种原因,即使它设置为永远运行,我也没有收到任何错误或警告,对不起,如果显而易见,如果代码很难理解,我只是包含了它所有的功能。
答案 0 :(得分:0)
&#39;选择器(倒计时())&#39;被评估为零。所以,
1.Move&#39;倒计时&#39;功能到touchesBegin之外。
2.使用选择器:&#34;倒计时&#34;&#39;而不是&#39;选择器(倒计时())&#39;
3.调用四次后使计时器失效。
var time = 4
var timer = NSTimer()
func countdown() {
if --time == 0 { timer.invalidate() }
println( "\(time)" )
}
func X() {
timer = NSTimer.scheduledTimerWithTimeInterval( 1, target: self, selector: "countdown", userInfo: nil, repeats: true)
}