Page 1 of 1

Animations

Posted: Sat Apr 16, 2011 3:11 pm
by Anxiety
How can i make simple image animations without having to use a loop to show different images at an order?

Thanks!

Re: Animations

Posted: Sat Apr 16, 2011 4:00 pm
by kikito
Actually, in most cases you don't need a loop. The basis of animation is changing an image on every love.update() call:

Code: Select all

  love.load()
    animation = {
      love.graphics.load('image1.png'),
      love.graphics.load('image2.png'),
      love.graphics.load('image3.png')
    }
    frame = 1 -- loops from 1 to 3
    timepassed = 0 -- how much time has passed since the last frame
  end

  love.update(dt)
    timepassed = timepassed + dt
    if timepassed >= 0.5 then
      timepassed = 0
      frame = frame + 1
      if frame == 4 then frame = 0 end
    end
  end

  love.draw()
    love.graphics.draw(animation[frame], 100, 200)
  end
I've tried to keep the code as simple as possible. You may notice that there are no loops.

Here are some things you should take into account:
  • once you are familiar with images and you have a working example, give a look at Quads. They are faster and more appropriate to animation-related stuff
  • Bartbe's AnAL lib simplifies this work a lot. Spend some time learning it.