Page 1 of 1

Problems playing animation

Posted: Fri Jan 15, 2016 4:16 pm
by robertjawoods
Hi guys, pretty new to game development and very new to Love/Lua (started Wednesday) and I'm having an issue playing an animation I've created.

My animation is in a 5x3 spritesheet with each sprite having dimensions of 96x96. Here is a function I created to return each sprite in an array of quads:

Code: Select all

function Animation:CreateQuads(spriteWidth, spriteHeight, rows, columns, img)
  frame = 1
  local Animation = {}
  xPos = 0
  yPos = 0

  for _ = 1,rows do
    for __ = 1,columns do
      Animation[frame] = love.graphics.newQuad(xPos, yPos, spriteWidth, spriteHeight, img:getWidth(), img:getHeight())
      xPos = xPos + spriteWidth
      frame = frame + 1
    end
    xPos = 0
    yPos = yPos + spriteHeight
  end

  return Animation
end
And here's my code for playing the animation:

Code: Select all

function Animation:PlayAnimation(animationArray, x, y, img)
  for frame = 1, #animationArray do
    love.graphics.draw(img, animationArray[frame], x, y)
  end
end
At the moment the animation is only implemented for when the player dies, but the animation seems to only display one frame.

I've attached my .love file so you can compile & see what I mean or have a pick through the rest of my code if you wish. Any help would be greatly appreciated, thank you.

Re: Problems playing animation

Posted: Fri Jan 15, 2016 5:25 pm
by s-ol
Your for loop only runs one frame. When you call :PlayAnimation, what happens is that you loop through all the animation frames, draw them on top of each other for one frame and forget about it.

Instead you need to store the current animation-frame and if enough time passes, move to the next one on update.

Re: Problems playing animation

Posted: Fri Jan 15, 2016 7:54 pm
by robertjawoods
I've edited my code to include

Code: Select all

frame = 1
    if not Player.isAlive then
        if etime ~= nil and stime ~= nil then
            if etime - stime > 0.16 then
                frame = frame + 1
            end
        end
    end
and

Code: Select all

stime = love.timer.getTime()
        etime = Animation:PlayAnimation(explosionAnimation, Player.x, Player.y, explosionSpritesheet, frame)

but still no dice. PlayAnimation now returns the value of love.timer.getTime. Any pointers?

Re: Problems playing animation

Posted: Sat Jan 16, 2016 12:41 am
by s-ol
First of all I just noticed you are using the colon function syntax (Animation:CreateQuads(), Animation:PlayAnimation()) wrongly, take a look at this: http://www.lua.org/pil/16.html

I would do it something like this:

Code: Select all

-- on animation update
function Animation:update(dt)
  self.timer = self.timer - dt
  if self.timer < 0 then
    self.frame = [self.frame % #self.frames]+1
    self.timer = self.timer + 0.13 -- seconds per frame
  end
end

-- on animation draw
function Animation:draw(x, y)
  love.graphics.draw(self.frames[self.frame], x, y)
end

Re: Problems playing animation

Posted: Sun Jan 17, 2016 1:08 pm
by robertjawoods
Got it... thanks for your help :)