Updating a timer delay on runtime?

Hello,

I have started working on a new project with Corona SDK and I have come into a problem that a Google search can’t seem to resolve.

Anyway what I needed to do is change the delay of a timer whilst the game is running. Seeing that I am a newbie I have tried the most logical solution that comes to my mind.

[lua]

timerDelay = 1000;

local function myFunction()
print(“Hello!”)
end

myTimer = timer.performWithDelay( timerDelay, myFunction, 0)

[/lua]

And by changing the ‘timerDelay’ variable it would change the delay on the timer. But it does not.

My second attempt was this:

[lua]
testVariable = 10;

local function myFunction()
print(“Hello!”)
end

myTimer = timer.performWithDelay( 1000, myFunction, 0)

if( testVariable == 10 ) then
myTimer.delay = 500
print(“Goodbye!”)
end

[/lua]

And using the .delay doesn’t work (at least for me anyway).

Is there a way to accomplish this short of cancelling the timer and starting a new timer with a new delay?

Thanks for any help.

Jamie

[import]uid: 170292 topic_id: 36364 reply_id: 336364[/import]

You’ll have to cancel the timer and recreate it with the new value. [import]uid: 33275 topic_id: 36364 reply_id: 144411[/import]

Or you could create your own timer in an enterFrame function, like this

interval = 2000  
local startTime = system.getTimer()  
  
local frameLoop = function(event)  
 if (event.time - startTime) \> interval then  
 print("do something")  
 startTime = event.time  
 end  
end -- frameLoop  
  
Runtime:addEventListener("enterFrame", frameLoop)  

Written of the top of my head and untested, but if you change the “interval” value you can set the timing yourself.

edit: I made interval a global variable on purpose, so you can set it from any place. Don’t be afraid to use global values with moderation, by the way - it’s good to always try to do things with locals, but if the situation fits a global, it fits a global value. [import]uid: 70134 topic_id: 36364 reply_id: 144427[/import]

You’ll have to cancel the timer and recreate it with the new value. [import]uid: 33275 topic_id: 36364 reply_id: 144411[/import]

Or you could create your own timer in an enterFrame function, like this

interval = 2000  
local startTime = system.getTimer()  
  
local frameLoop = function(event)  
 if (event.time - startTime) \> interval then  
 print("do something")  
 startTime = event.time  
 end  
end -- frameLoop  
  
Runtime:addEventListener("enterFrame", frameLoop)  

Written of the top of my head and untested, but if you change the “interval” value you can set the timing yourself.

edit: I made interval a global variable on purpose, so you can set it from any place. Don’t be afraid to use global values with moderation, by the way - it’s good to always try to do things with locals, but if the situation fits a global, it fits a global value. [import]uid: 70134 topic_id: 36364 reply_id: 144427[/import]

You’ll have to cancel the timer and recreate it with the new value. [import]uid: 33275 topic_id: 36364 reply_id: 144411[/import]

Or you could create your own timer in an enterFrame function, like this

interval = 2000  
local startTime = system.getTimer()  
  
local frameLoop = function(event)  
 if (event.time - startTime) \> interval then  
 print("do something")  
 startTime = event.time  
 end  
end -- frameLoop  
  
Runtime:addEventListener("enterFrame", frameLoop)  

Written of the top of my head and untested, but if you change the “interval” value you can set the timing yourself.

edit: I made interval a global variable on purpose, so you can set it from any place. Don’t be afraid to use global values with moderation, by the way - it’s good to always try to do things with locals, but if the situation fits a global, it fits a global value. [import]uid: 70134 topic_id: 36364 reply_id: 144427[/import]

You’ll have to cancel the timer and recreate it with the new value. [import]uid: 33275 topic_id: 36364 reply_id: 144411[/import]

Or you could create your own timer in an enterFrame function, like this

interval = 2000  
local startTime = system.getTimer()  
  
local frameLoop = function(event)  
 if (event.time - startTime) \> interval then  
 print("do something")  
 startTime = event.time  
 end  
end -- frameLoop  
  
Runtime:addEventListener("enterFrame", frameLoop)  

Written of the top of my head and untested, but if you change the “interval” value you can set the timing yourself.

edit: I made interval a global variable on purpose, so you can set it from any place. Don’t be afraid to use global values with moderation, by the way - it’s good to always try to do things with locals, but if the situation fits a global, it fits a global value. [import]uid: 70134 topic_id: 36364 reply_id: 144427[/import]