Page 1 of 1
one second timer
Posted: Sat Aug 31, 2013 3:36 am
by lolsasory
how would i say every 1 second add 1 to x?
Re: one second timer
Posted: Sat Aug 31, 2013 3:41 am
by Jasoco
There are a few timer libraries out there. Look up Cron I believe. Or Hump which has a Timer part.
Cron:
http://love2d.org/forums/viewtopic.php?f=5&t=2882
Hump:
http://love2d.org/forums/viewtopic.php?f=5&t=1795
With Cron you'd use the cron.every to create a simple function that would perform your addition task every second.
Re: one second timer
Posted: Sat Aug 31, 2013 6:09 am
by Plu
If you want a basic function, use two variables like this. (It goes in love.update)
One of them is integer based and the other is a float that collects pieces of passing time from the dt that love.update() gets. Then every time more than a second has been collected, that seconds gets moved to the other function.
(You'll have to define baseTimer and seconds, they need to start at 0 )
Code: Select all
baseTimer = baseTimer + dt
if baseTimer > 1 then
baseTimer = baseTimer - 1
seconds = seconds + 1
end
Alternatively, if it's just about the number, you can also just count time as a float and then use its rounded-down version:
Code: Select all
baseTimer = baseTimer + dt
-- now you can use math.floor( baseTimer ) to get the number of seconds as an integer