How to make 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.
LavX64
Prole
Posts: 8
Joined: Wed Sep 23, 2015 9:44 am

How to make animation?

Post 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?
Attachments
Project.love
(13.94 MiB) Downloaded 126 times
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: How to make animation?

Post 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 117 times
Last edited by MadByte on Thu Sep 24, 2015 2:31 pm, edited 1 time in total.
LavX64
Prole
Posts: 8
Joined: Wed Sep 23, 2015 9:44 am

Re: How to make animation?

Post by LavX64 »

Oh sh**. I should really use ani8 ^^
But anyway, thank you very much :3
LavX64
Prole
Posts: 8
Joined: Wed Sep 23, 2015 9:44 am

Re: How to make animation?

Post 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?
Attachments
Project.love
Some error(and i dont know why)
(13.97 MiB) Downloaded 111 times
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: How to make animation?

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

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
LavX64
Prole
Posts: 8
Joined: Wed Sep 23, 2015 9:44 am

Re: How to make animation?

Post 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.
LavX64
Prole
Posts: 8
Joined: Wed Sep 23, 2015 9:44 am

Re: How to make animation?

Post by LavX64 »

Ah, anyway i can't do it properly. Help me plz :C
Attachments
Project.love
(13.97 MiB) Downloaded 122 times
User avatar
NickRock
Citizen
Posts: 76
Joined: Thu Dec 25, 2014 9:33 pm
Location: Earth
Contact:

Re: How to make animation?

Post 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.
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
Fang86
Prole
Posts: 15
Joined: Fri Sep 25, 2015 2:03 am

Re: How to make animation?

Post 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.
LavX64
Prole
Posts: 8
Joined: Wed Sep 23, 2015 9:44 am

Re: How to make animation?

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

Who is online

Users browsing this forum: Google [Bot] and 166 guests