This class provides an implementation of timers.
Requirements
- Class Plugin
Creation
- Timer(duration, callback)
- Creates and returns a new timer instance. The timer expires after the specified duration in milliseconds. It calls the function callback on this event. After the creation the timer is stopped and has to be started by calling Timer:start().
Methods
- Timer:start()
- Starts the timer.
- Timer:stop()
- Stops the timer. The elapsed time is saved.
- Timer:reset()
- Resets the timer.
- Timer:update()
- Updates the timer. If time is up it will call the callback function with the timer object as argument.
Example
scrupp.init("Timer Test", 600, 400, 32, false)
-- load required plugins
scrupp.addFile("scripts/class.lua")
scrupp.addFile("scripts/font.lua")
scrupp.addFile("scripts/timer.lua")
local font = Font("fonts/Vera.ttf", 20)
-- this variable is used to distinguish between tick and tack :)
local tick = true
-- create a new timer with a duration of 1 second
-- (= 1000 milliseconds)
-- the callback negates the tick variable
local timer = Timer(1000, function(timer)
tick = not tick
end)
-- start the timer
timer:start()
main = {
render = function(dt)
-- update the timer
timer:update()
-- print Tick or Tack depending on the state of the
-- tick variable
if tick then
font:print(10,10, "Tick!")
else
font:print(10,10, "Tack!")
end
end
}