Gradually reduce delay of a timer

I have:

timer.performWithDelay(1000, spawn, 0)  

I would like to have this delay decrease over time.

For example, every 5 seconds I would like the delay to be 100 miliseconds less.

So after five seconds, it changes to 900.
After another five seconds it changes to 800.
And so on.

I have tried creating a variable and every 5 seconds decrementing the variable, but this does not appear to work. Any ideas?

Thanks a lot in advance! [import]uid: 9968 topic_id: 9835 reply_id: 309835[/import]

You could try something like this:

[code]local spawnTimer = 1000

local function spawn()

– Your code here

timer.performWithDelay(spawnTimer, spawn)
end

local function spawnCounterIncrement()
spawnTimer = spawnTimer - 100
end

timer.performWithDelay(spawnTimer, spawn)
timer.performWithDelay(5000, spawnCounterIncrement, 0)[/code]

This is just an example, you’ll need to add to it to make it fully functional. To escape the loop, you can have a conditional statement before calling the timer. You’ll also want to make sure to add some more code to make sure that spawnTimer never gets below 1. [import]uid: 14598 topic_id: 9835 reply_id: 35833[/import]

chinmay.patil… doesn’t work at all :slight_smile:
This one does, and includes test codes :smiley:

[lua]function main()
local timeDec = 100
local thisTime = 1000
local oldTime,newTime = system.getTimer(),0 --test
local i = 0 --test
local function spawn()
newTime = system.getTimer() --test
print("Spawned at "…newTime,"Delay = "…newTime - oldTime…“ms”,“I:”…i,"Next Delay = "…thisTime) --test
i = i + 1 --test
oldTime = newTime --test
spawnTimer = timer.performWithDelay(thisTime, spawn, 1)
end
timer.performWithDelay(5000,function()
thisTime = thisTime - timeDec
if thisTime < 100 then thisTime = 100 end --cap at 100, else it sets the delay to 0, which performs it an infinite amount of times.
end,0)
spawn()
end
main()[/lua] [import]uid: 34945 topic_id: 9835 reply_id: 35884[/import]

Don’t use a timer for this, it is far more efficient and easier to just register a runtime event.

As I’m in a good mood, here’s an untested sample code:
[lua]local timerTime = system.getTimer()
local spawnTime = system.getTimer()
local elapsedTime
local spawnDelay = 1000

local function spawn(event)
elapsedTime = event.time - spawnTime

if elapsedTime > spawnDelay then
spawnTime = event.time
– spawn the enemy
end

elapsedTime = event.time - timerTime

if elapsedTime > 5000 then
timerTime = event.time
spawnDelay = spawnDelay - 100

if spawnDelay == 0 then
Runtime:removeEventListener( “enterFrame”, spawn )
end
end
end

Runtime:addEventListener( “enterFrame”, spawn )[/lua] [import]uid: 51516 topic_id: 9835 reply_id: 35895[/import]

. [import]uid: 48521 topic_id: 9835 reply_id: 35845[/import]

Hey Seth,

I’m relatively new to LUA/Corona programming. Can you explain how the Runtime solution is more efficient than using timers? It seems to be that it would take more for Corona to do some math and conditional checks on each EnterFrame event, but is it just that the timer functions are that inefficient? I’d love a clearer idea about which to use in my apps.

Thanks,

Alan [import]uid: 14598 topic_id: 9835 reply_id: 35913[/import]

Timers should be used for what they are designed, a one time delay. If you need them, use them. But if you’re going to loop using timers or do advanced stuff, don’t. In your case, you won’t probably notice much difference but your code might evolve and it’s always better to have good foundations. [import]uid: 51516 topic_id: 9835 reply_id: 35918[/import]

Great, thanks for the advice Seth! I’m updating my code now to use the Runtime more before things get too crazy. [import]uid: 14598 topic_id: 9835 reply_id: 35926[/import]

Lol, I think I just got it. Not that my game has anything to do with fast paced action :stuck_out_tongue:

Seth. Does enterFrame trigger once per frame draw?
I.e. every fps (terrible english)

I hadn’t even looked at it twice, assuming it was something to do with the viewport ‘frame’

[import]uid: 34945 topic_id: 9835 reply_id: 35970[/import]

That is correct. [import]uid: 51516 topic_id: 9835 reply_id: 35975[/import]

The possibilities are endless. [import]uid: 34945 topic_id: 9835 reply_id: 35992[/import]

Hey Seth,

Works like a charm; however, I cant quite figure out how to allow my pause functionality to work well with this.

I have a variable isActive to keep track.

Basically if I pause the game, the runtime event continues and when I resume, all spawned items appear at once. Need it to pause the reduction in time as well.

After a lot of experiments, I cannot get it to perform as expected. Any idea’s?

Thanks a lot! [import]uid: 9968 topic_id: 9835 reply_id: 36024[/import]

What you need to do is in your pause function, note the event.time
and when you unpause is note the event.time for that, diff should give you how long the game was paused.

You need to subtract this time period from elapsedTime before checking if it’s time to spawn new object. [import]uid: 48521 topic_id: 9835 reply_id: 36028[/import]

What you need to do is in your pause function, note the event.time
and when you unpause is note the event.time for that, diff should give you how long the game was paused.

You need to subtract this time period from elapsedTime before checking if it’s time to spawn new object. [import]uid: 48521 topic_id: 9835 reply_id: 36029[/import]

hi there,

I am new to lua and corona… I was trying out one of the samples regarding chaging the gear rotation speed demo…

everything works fine… but when the stop button is clicked i want the animation to gradually stop as opposed to just stopping suddenly… I added a delay function… which works… but the animation on the screen still stops straight away… what am I doing wrong?


local ui = require(“ui”)

local background = display.newImage(‘background.png’)

local gear = display.newImage(‘gear.jpg’)

gear.xScale = .5
gear.yScale = .5

gear.y = display.contentHeight/2
gear.x = display.contentWidth/2

local rotateAmount = 1

local function animate(event)
gear.rotation = gear.rotation + rotateAmount
gear.rotation = gear.rotation + rotateAmount
end
local function SlowRotation()

print (rotateAmount)

if rotateAmount > 0 then
rotateAmount = rotateAmount -1
elseif rotateAmount < 0 then
rotateAmount = rotateAmount + 1
end

end

local function wait(millisecond)
local timerTime = system.getTimer()
timerTime = timerTime + millisecond
local timeNow = system.getTimer()

while timeNow < timerTime do

timeNow = system.getTimer()
end
SlowRotation();
end

local buttonHandler = function (event)

if event.phase == “release” then
if event.id == “stop” then

while rotateAmount ~= 0 do
wait(2000)
end
elseif event.id == “+1” then
rotateAmount = rotateAmount + 1
elseif event.id == “-1” then
rotateAmount = rotateAmount -1
elseif event.id == “reverse” then
rotateAmount = rotateAmount * -1
end
end

end

Runtime:addEventListener(“enterFrame”, animate)

local button1 = ui.newButton{
default = “buttonGray.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = “Stop”,
id=“stop”,
size=12,
emboss=true
}

local button2 = ui.newButton{
default = “buttonGray.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = “+1”,
id="+1",
size=12,
emboss=true
}

local button3 = ui.newButton{
default = “buttonGray.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = “-1”,
id="-1",
size=12,
emboss=true
}

local button4 = ui.newButton{
default = “buttonGray.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = “Reverse”,
id=“reverse”,
size=12,
emboss=true
}

button1.x = (display.contentWidth / 4 * 1) - display.contentWidth / 8; button1.y = display.contentHeight - 20
button2.x = (display.contentWidth / 4 * 2) - display.contentWidth / 8; button2.y = display.contentHeight - 20
button3.x = (display.contentWidth / 4 * 3) - display.contentWidth / 8; button3.y = display.contentHeight - 20
button4.x = (display.contentWidth / 4 * 4) - display.contentWidth / 8; button4.y = display.contentHeight - 20 [import]uid: 67619 topic_id: 9835 reply_id: 42484[/import]

Well, for a start, use the lua or code tag
:slight_smile:
[lua]local ui = require(“ui”)

local background = display.newImage(‘background.png’)

local gear = display.newImage(‘gear.jpg’)
gear.xScale = .5
gear.yScale = .5

gear.y = display.contentHeight/2
gear.x = display.contentWidth/2

local rotateAmount = 1

local function animate(event)
gear.rotation = gear.rotation + rotateAmount
gear.rotation = gear.rotation + rotateAmount
end

local function SlowRotation()

print (rotateAmount)

if rotateAmount > 0 then
rotateAmount = rotateAmount -1
elseif rotateAmount < 0 then
rotateAmount = rotateAmount + 1
end

end

local function wait(millisecond)

local timerTime = system.getTimer()
timerTime = timerTime + millisecond
local timeNow = system.getTimer()

while timeNow < timerTime do

timeNow = system.getTimer()
end
SlowRotation();
end

local buttonHandler = function (event)

if event.phase == “release” then
if event.id == “stop” then

while rotateAmount ~= 0 do
wait(2000)
end
elseif event.id == “+1” then
rotateAmount = rotateAmount + 1
elseif event.id == “-1” then
rotateAmount = rotateAmount -1
elseif event.id == “reverse” then
rotateAmount = rotateAmount * -1
end
end

end

Runtime:addEventListener(“enterFrame”, animate)

local button1 = ui.newButton{
default = “buttonGray.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = “Stop”,
id=“stop”,
size=12,
emboss=true
}

local button2 = ui.newButton{
default = “buttonGray.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = “+1”,
id="+1",
size=12,
emboss=true
}

local button3 = ui.newButton{
default = “buttonGray.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = “-1”,
id="-1",
size=12,
emboss=true
}

local button4 = ui.newButton{
default = “buttonGray.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = “Reverse”,
id=“reverse”,
size=12,
emboss=true
}

button1.x = (display.contentWidth / 4 * 1) - display.contentWidth / 8; button1.y = display.contentHeight - 20
button2.x = (display.contentWidth / 4 * 2) - display.contentWidth / 8; button2.y = display.contentHeight - 20
button3.x = (display.contentWidth / 4 * 3) - display.contentWidth / 8; button3.y = display.contentHeight - 20
button4.x = (display.contentWidth / 4 * 4) - display.contentWidth / 8; button4.y = display.contentHeight - 20[/lua] [import]uid: 34945 topic_id: 9835 reply_id: 42522[/import]