Animations

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Animations

Post by Anxiety »

How can i make simple image animations without having to use a loop to show different images at an order?

Thanks!
I can't come up with a good signature!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Animations

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

Who is online

Users browsing this forum: No registered users and 3 guests