Hello,
I'm having problems with timers on playdate SDK. I'm trying to create a standar timer to update certain part of UI when no button has been pressed for 5 seconds.
I'm using [playdate.timer.new(duration, callback, ...)](file:///D:/Projects/PlaydateSDK/Inside%20Playdate.html#C-commonTimerMethods) function to create my timer and I've created a function to update UI to use as callback:
import 'CoreLibs/timer'
--
local timeToUpdateLife = 5 * 1000
local lifeTimer = nil
--
function updateLifeHistory()
for i in ipairs(history) do
history[i]:updateLife(players[i].life)
end
end
function resetLifeTimer()
lifeTimer = playdate.timer.new(timeToUpdateLife , updateLifeHistory())
end
--
function playdate.update()
gfx.sprite.update()
playdate.timer.updateTimers()
end
I want the function updateLifeHistory() to be trigger 5 seconds after I press some button. The function playdate.timer.updateTimers() is called on playdate.update().
I update my timer everytime a player press up or down button using playdate.downButtonDown() and playdate.upButtonDown()
function playdate.upButtonDown()
local function timerCallback()
life = players[activePlayer].life + 1
players[activePlayer]:setLife(life)
resetLifeTimer()
end
keyTimer = playdate.timer.keyRepeatTimer(timerCallback)
end
function playdate.upButtonUp() keyTimer:remove() end
function playdate.downButtonDown()
local function timerCallback()
life = players[activePlayer].life - 1
players[activePlayer]:setLife(life)
resetLifeTimer()
end
keyTimer = playdate.timer.keyRepeatTimer(timerCallback)
end
function playdate.downButtonUp() keyTimer:remove() end
The expected behaviour is to trigger the resetLifeTimer() function 5 seconds after I press up or down keys. But the function is called inmediatelly after I press these buttons. Everytime I presse these keys, the function updateLifeHistory() is called with no delay.
Is there something I'm missing using regultar timers? I don't understand this behaviour.
Thank you for your help.
Regards,