Add a delay / timer
I am currently using Corona SDK, Lua as my main language. I have a problem with this code where - when I run it, it automatically gives me the "light" values ββin which I stated I was printing. I set light = 2 and with this loop, it should decrease the light by 1 every time until it becomes <= 0. When I run the program, the values ββare displayed as 1.0, -1 all at the same time. I was wondering if I could add a delay between all values.
I'm making a game called Simon Says, and it doesn't light up the boxes because it starts everything at once.
Here is the code:
if(count%20 == count - math.floor(count/20)*20) then
clicked = 0
while(light >= 0) do
light = light - 1
print(light)
end
end
source to share
Here is a simple function timer.performWithDelay from the Corona SDK. You can see more here: https://docs.coronalabs.com/api/library/timer/performWithDelay.html
Below is some sample code that matches your question.
Note . I based the code on the above code.
local lights = 2
local timerName -- ADD A TIMER ID TO YOUR TIMER SO THAT WE CAN CANCEL IT LATER ON
local function myFunction()
lights = lights - 1
if (lights >= 0) then
--DO SOMETHING WITH THE LIGHTS
print(lights)
else
--TERMINATE THE TIMER
timer.cancel( timerName )
end
end
if(count%20 == count - math.floor(count/20)*20) then
timerName = timer.performWithDelay( 1000, myFunction, 0 )
end
source to share