Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to change delay time on timer during run time.
Started by mickeyappiah33 Feb 18 2019 09:43 AM

4 replies to this topic
timer
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

mickeyappiah33

[GLOBAL: userInfoPane.html]
mickeyappiah33
  • Observer

  • 4 posts
  • Corona SDK

Hi, I am a newbie and this is my first time here so please go easy on me,lol.
On to my question. I have a timer that calls a function that throws balloons every 3 seconds forever,how do I shorten the delay time so the balloons are thrown faster?
I tried replacing the delay time with a variable so i can change it if conditions are met in the balloon throwing function, but i does not work.
I know the value changes as I want it because I output it. But the timer ignores it. Can the timer parameters be defined only before run time?

Example:

local count = 0
local delayTime = 3000

local function balloonFunction()

if(count > 5)then
delayTime = 2000
end

throwBallon()
count = count + 1

end

local throwBalloonLoop = timer.performWithDelay(delayTime, balloonFunction, 0)

[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,708 posts
  • Corona SDK

You have to cancel the timer, and then restart it with the new value. In order to do that you need to save the reference to the timer, in a scope where it can be seen by the function that needs to amend it.

 

 
local myTimer =timer.performWithDelay(myDelay, myFunction)
 
-- later on...
 
timer.cancel(myTimer)
myTimer = nil
myTimer =timer.performWithDelay(myDelay, myFunction)
 

 

How I usually do it is to roll my own timer.

 

 

 
local myDelay = 3000
local time = system.getTimer()
 
 
local myTimedFunction ()
 
 -- do my timed stuff here
 
end
 
local gameLoop = function ()
 
 local t = system.getTimer()
 
if t - time >= myDelay then
  myTimedFunction()
  time = system.getTimer()
  myDelay = myDelay - 100
 
end
 
 
end
 
Runtime:addEventListener("enterFrame", gameLoop)
 



[TOPIC: post.html]
#3

mickeyappiah33

[GLOBAL: userInfoPane.html]
mickeyappiah33
  • Observer

  • 4 posts
  • Corona SDK

Thanks, that was quick. I will try this immediately.

[TOPIC: post.html]
#4

mickeyappiah33

[GLOBAL: userInfoPane.html]
mickeyappiah33
  • Observer

  • 4 posts
  • Corona SDK

Thanks a million, it works!!!

[TOPIC: post.html]
#5

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 184 posts
  • Corona SDK

Hello,

Just change the event._delay parameter on the function.

Sincerely,
Yvan.


[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: timer