Problems playing animation

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.
Post Reply
robertjawoods
Prole
Posts: 6
Joined: Fri Jan 15, 2016 3:55 pm

Problems playing animation

Post 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.
Attachments
Shooter.love
(694.56 KiB) Downloaded 76 times
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Problems playing animation

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
robertjawoods
Prole
Posts: 6
Joined: Fri Jan 15, 2016 3:55 pm

Re: Problems playing animation

Post 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?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Problems playing animation

Post 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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
robertjawoods
Prole
Posts: 6
Joined: Fri Jan 15, 2016 3:55 pm

Re: Problems playing animation

Post by robertjawoods »

Got it... thanks for your help :)
Post Reply

Who is online

Users browsing this forum: bostonstrong567, Google [Bot], Semrush [Bot] and 4 guests