Change dt or use another timeformat?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Change dt or use another timeformat?

Post by kikito »

:ultrahappy: I see where the problem is.

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.
Then, the other problem. You must understand that love.update is called once per frame. This means, every time your game refreshes on the screen. That's many times per second.

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
Since you only needed one clock, I've created it inside love.load instead of love.update. love.load() is called only once at the beginning of your game, unlike love.update.

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)
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests