Arthur Macpherson Arthur Macpherson - 26 days ago 10
Swift Question

Swift 3 Update the variable which represents the time interval between a function being called

I have created a function and it is called every 27 seconds. The code for calling the variable is as follows

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


The variable
time
is multiplied by 0.95 in the function method but the variable time is still not updated.

Answer

timeInterval you have specify with scheduledTimer is remain constant for that scheduled Timer object if you want to change time then you need to schedule it again in that calling function like this.

 _ = 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)
 }