Swift 3 更新表示调用函数之间的时间间隔的变量
Swift 3 Update the variable which represents the time interval between a function being called
我创建了一个函数,它每 27 秒调用一次。调用变量的代码如下
_ = Timer.scheduledTimer(timeInterval: time, target: self, selector: #selector(GameScene.method), userInfo: nil, repeats: true)
变量time
在函数方法中乘以0.95但变量时间仍然没有更新。
timeInterval
你用 scheduledTimer
指定的那个预定的 Timer
对象保持不变,如果你想改变时间,那么你需要像这样在调用函数中再次安排它.
_ = Timer.scheduledTimer(timeInterval: time, target: self, selector: #selector(GameScene.method), userInfo: nil, repeats: false)
func method() {
//Do your task
time += 0.95 //increase timer
//Schedule it again
_ = Timer.scheduledTimer(timeInterval: time, target: self, selector: #selector(GameScene.method), userInfo: nil, repeats: false)
}
func increaseTime() {
time += 0.95 // This will increase the time by 0.95 every time this function is called
// Then call the function again to update the time
_ = Timer.scheduledTimer(timeInterval: time, target: self, selector: #selector(GameScene.method), userInfo: nil, repeats: true)
}
确保您的变量是 var
而不是 let
或 const
,因为常量变量无法编辑。
我创建了一个函数,它每 27 秒调用一次。调用变量的代码如下
_ = Timer.scheduledTimer(timeInterval: time, target: self, selector: #selector(GameScene.method), userInfo: nil, repeats: true)
变量time
在函数方法中乘以0.95但变量时间仍然没有更新。
timeInterval
你用 scheduledTimer
指定的那个预定的 Timer
对象保持不变,如果你想改变时间,那么你需要像这样在调用函数中再次安排它.
_ = Timer.scheduledTimer(timeInterval: time, target: self, selector: #selector(GameScene.method), userInfo: nil, repeats: false)
func method() {
//Do your task
time += 0.95 //increase timer
//Schedule it again
_ = Timer.scheduledTimer(timeInterval: time, target: self, selector: #selector(GameScene.method), userInfo: nil, repeats: false)
}
func increaseTime() {
time += 0.95 // This will increase the time by 0.95 every time this function is called
// Then call the function again to update the time
_ = Timer.scheduledTimer(timeInterval: time, target: self, selector: #selector(GameScene.method), userInfo: nil, repeats: true)
}
确保您的变量是 var
而不是 let
或 const
,因为常量变量无法编辑。