First of all, two small problems:
- It's a good idea to start every file with the require '...' it needs. That way, it will be clear what "other files" it needs.
- You are defining love.draw inside love.update. That isn't a good idea.
On the other hand, the cron.update is supposed to be called once per clock needed. If you need one action to repeat periodically, you just call it once. On the previous example, if you were creating, say, 10 trolls, you would call it 10 times only - once per troll.
Since you were calling cron.every directly inside love.update, you were creating one clock every frame. After the first second, lots of clocks started to "tick" a - and it went faster every second!
But you need to call it only once.
Here's a revised version of your code
Code: Select all
cron = require 'cron'
function love.load()
a = 1
aClock = cron.every(1, function()
a = a + 1
end)
end
function love.update(dt)
cron.update(dt) -- you must do this once inside love.update
end
function love.draw()
love.graphics.print(a, 200, 200)
end
This should do the trick. I propose that you now add a function to stop the count when a key is pressed (hint: use the aClock variable and cron.cancel)