Swift 3更新表示被调用函数之间的时间间隔的变量

问题描述:

我创建了一个函数,并且每27秒调用一次该函数。用于调用该变量的代码如下Swift 3更新表示被调用函数之间的时间间隔的变量

_ = Timer.scheduledTimer(timeInterval: time, target: self, selector: #selector(GameScene.method), userInfo: nil, repeats: true) 

可变time由0.95在函数相乘方法,但仍没有更新可变时间。

+0

检查该方法是否正在调用。 –

+0

我很抱歉,你的意思是?该方法正在调用中,但它每27秒调用一次,而不是变量的更新版本 –

+0

请向我们展示方法及其代码。 –

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) 
} 
+0

您需要先使计时器无效,否则最终会在每次方法调用中使用多个计时器。 – zisoft

+0

@zoftoft检查正确我已设置重复假' –

+0

你是对的;-) – zisoft

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不是letconst因为常数变量不能进行编辑。