You can use modulo like this if you are getting os.time() instead of dt, because os.time() (at least on windows and linux) returns an INT. You can also, alternatively, floor the return of love.timer.getTime(), which I think would be more compatible with other platforms where what os.time() returns may not be consistent. I use it all over the place for simple animations (things that pulse on a frequency of 0.5 or 0.33hz, for instance). For anything that needs a frequency outside of that, use a timer instead.
You don't need the function to be aware of dt in this case as it can be using only local vars, but I tend to have a ton of timers that are all global anyway all over the place.
For instance, in my current game, here is a simple piece of a minimap drawing function that makes the player's icon blink 1 time per second by using os.time() or math.floor(love.timer.getTime():
Code: Select all
if ((mmx == smx + 1) and (mmy == smy + 1)) then -- player is here
local t = math.floor(love.timer.getTime())
if (t % 2 ~= 0) then
local poX = (global.game.player.r_x * 64) - 64
local poY = (global.game.player.r_y * 64) - 64
love.graphics.setColor(1,1,1,1)
love.graphics.draw(images.yellow64, mmox+poX, mmoy+poY, 0, 1, 1)
end
end
Another way you can do it is with a heartbeat timer. I have a global dt timer that simply flaps between 0-1 at all times, and I know that any function can read the heartbeat to do simple timing stuff:
Code: Select all
if (heartbeatrise) then
heartbeat = heartbeat + dt
if (heartbeat >= 1) then
heartbeat, heartbeatrise = 1, false
end
else
heartbeat = heartbeat - dt
if (heartbeat <= 0) then
heartbeat, heartbeatrise = 0, true
end
end
I use it to do things like these pulsing bars here:
https://love2d.org/imgmirrur/PLgl6C2.mp4