The Timer Class executes a call back function at the end of the time event. Providing as an argument “id” value.
Which is received in the handler (handlerTimer(id) as a nil value.
Is there a way to handle this?
-- Main.lus
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
myTimer1 = Timer(1, function() handlerTimer(id) end)
myTimer2 = Timer(2, function() handlerTimer(id) end)
myTimer1:start(1.2)
myTimer2:start(1.0)
end
-- This function gets called once every frame
function draw()
myTimer1:update()
myTimer2:update()
end
-- This handler is called at the end of a timer event
function handlerTimer(id) --> id is a nil
print("Timer " .. id .. " time out")
end
-- Timer.lus
Timer = class()
function Timer:init(id, callBackFun)
-- you can accept and set parameters here
self.id = id
self.action = callBackFun
self.startTime = 0
self.timeDelay = 0
self.enable = false
end
function Timer:start(time)
self.startTime = ElapsedTime
self.timeDelay = time
self.enable = true
end
function Timer:update()
if (self.enable == true) then
local currentTime = ElapsedTime
if currentTime - self.startTime >= self.timeDelay then
self.enable = false
if type(self.action) == "function" then
self.action(self.id) --> return local copy of id
end
end
end
end