Page 1 of 2

How to make animation?

Posted: Thu Sep 24, 2015 8:37 am
by LavX64
I want to create animated death of enemies, and animations when they hit bullet. But i dont want to use any modules, because my animation is to easy for this(1-2 frames, mb 5). So, what should i do?

Re: How to make animation?

Posted: Thu Sep 24, 2015 12:06 pm
by MadByte
No animation is "to easy" to use a animation library :) (like anim8)
Anyway, if you really want to do it manually you could create a table which later contains all frames. set the image frame width and height and the spritesheet you want to use and also set a delay you would like to have. Then use love.graphics.newQuad for each frame in your image and save it to the table. Now you just need to update a timer which changes the current frame everytime the given "delay" is reached and if you got the last item in your table reset it to the first one.
You also need to create a state variable (playing, paused states).

I really recommend you to use a library ;)
here is a quick example for animation creation:

Code: Select all

love.graphics.setDefaultFilter("nearest", "nearest")

-- Create a new Animation Object --
-- Note: this method isn't that efficent! --
function newAnimation(imagePath, fw, fh, delay)
  local animation = {}
  animation.image = love.graphics.newImage(imagePath)
  animation.fw = fw
  animation.fh = fh
  animation.delay = delay or .1
  animation.timer = 0
  animation.quads = {}
  animation.state = "paused"
  animation.currentFrame = 1
  
  local iw, ih = animation.image:getDimensions()
  for i = 1, iw/fw do
    animation.quads[i] = love.graphics.newQuad(animation.fw * i - animation.fw, 0, animation.fw, animation.fh, animation.image:getDimensions())
  end
  
  animation.play = function(self)
    self.currentFrame = 1
    self.state = "playing"
  end
  
  animation.pause = function(self)
    self.state = "paused"
  end
  
  animation.setFrame = function(self, frame)
    self.currentFrame = frame
  end

  animation.resume = function(self)
    self.state = "playing"
  end
  
  animation.update = function(self, dt)
    self.timer = self.timer + dt
    if self.timer > self.delay and self.state == "playing" then
      if self.currentFrame < #self.quads then self.currentFrame = self.currentFrame+1
    else self.currentFrame = 1 end
    self.timer = 0
    end
  end
  
  animation.draw = function(self, x, y, sx, sy, ox, oy, kx, ky)
    love.graphics.draw(self.image, self.quads[self.currentFrame], x or 0, y or 0, sx or 1, sy or 1, ox or 0, oy or 0, kx or 0, ky or 0)
  end
  
  return animation
end


local men1 = newAnimation("spritesheet.png", 10, 12, .1)
men1:play()

function love.update(dt)
  men1:update(dt)
end

function love.draw()
  men1:draw(100, 100, 0, 4, 4)
end

function love.keypressed(key)
  if key == "escape" then love.event.quit() end
end
animation.love
(1.14 KiB) Downloaded 132 times

Re: How to make animation?

Posted: Thu Sep 24, 2015 2:21 pm
by LavX64
Oh sh**. I should really use ani8 ^^
But anyway, thank you very much :3

Re: How to make animation?

Posted: Thu Sep 24, 2015 2:57 pm
by LavX64
Well, now i got another problem.
"There is no frame for x=1, y=1"
I have watched tutorials here https://github.com/kikito/anim8, and made all step-by-step. Created framed sprite sheet, i know size of all canvas, i made all pixel-by-pixel, so i just cant understand where is the problem :( .
What can be the problem? Can u help me?

Re: How to make animation?

Posted: Thu Sep 24, 2015 3:05 pm
by s-ol
LavX64 wrote:Well, now i got another problem.
"There is no frame for x=1, y=1"
I have watched tutorials here https://github.com/kikito/anim8, and made all step-by-step. Created framed sprite sheet, i know size of all canvas, i made all pixel-by-pixel, so i just cant understand where is the problem :( .
What can be the problem? Can u help me?
Either your grid or image size is wrong, anim8 can't find the first sprite because of this.

Re: How to make animation?

Posted: Thu Sep 24, 2015 3:12 pm
by LavX64
S0lll0s wrote: Either your grid or image size is wrong, anim8 can't find the first sprite because of this.
Yep, but i made it pixel-by-pixel, so cant understand where problem exactly is.

Re: How to make animation?

Posted: Thu Sep 24, 2015 3:40 pm
by LavX64
Ah, anyway i can't do it properly. Help me plz :C

Re: How to make animation?

Posted: Fri Sep 25, 2015 3:13 pm
by NickRock
Hey man, the code is really messed up. I had to spend 30 minutes to understand what's going on..
Maybe you can try using AnAL (Animation And Love) Get it here: https://love2d.org/wiki/AnAL

It says that it's outdated but it's really easy for beginners in my opinion.

Re: How to make animation?

Posted: Mon Sep 28, 2015 4:03 am
by Fang86
NickRock wrote:Hey man, the code is really messed up. I had to spend 30 minutes to understand what's going on..
Maybe you can try using AnAL (Animation And Love) Get it here: https://love2d.org/wiki/AnAL

It says that it's outdated but it's really easy for beginners in my opinion.
Yeah I love AnAL :rofl: No but seriosuly, it's a great library for animation, also very simple to use.

Re: How to make animation?

Posted: Mon Sep 28, 2015 9:39 am
by LavX64
Yeeeep. My code so sh**ty ^^
But its my like 1st try, so i will grow up in this buisness. And thx for all your help) I really appreciate