![](/img/trans.png)
[英]Swift, XCode: Is there a way to use a variable as a NSTimer.scheduledTimerWithTimeInterval Int?
[英]NSTimer.scheduledTimerWithTimeInterval() with variable delay
我有一個函數,需要每x秒調用一次。 在開始時x = 5,但每次調用此函數時,x需要遞減一定數量。 我知道如果x為常數怎么辦:
override func didMoveToView(view: SKView) {
NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("function"), userInfo: nil, repeats: true)
}
func function(){
println("TEST")
}
如何減少每次調用函數之間的延遲?
我會這樣更改您的代碼:
var timeDuration: NSTimeInterval = 5
override func didMoveToView(view: SKView) {
NSTimer.scheduledTimerWithTimeInterval(timeDuration, target: self, selector: Selector("function"), userInfo: nil, repeats: false)
}
func function(){
println("TEST")
timeDuration -= 1
if timeDuration > 0{
NSTimer.scheduledTimerWithTimeInterval(timeDuration, target: self, selector: Selector("function"), userInfo: nil, repeats: false)
}
}
這應該工作,還沒有測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.