Animation while moving sprite?

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
ghostrunners
Prole
Posts: 8
Joined: Fri Jan 27, 2012 3:40 pm
Location: Georgia

Animation while moving sprite?

Post by ghostrunners »

I'm using the anim8 library to move a sprite around - https://github.com/kikito/anim8. The problem I encounter is when I hold onto a key to move sprite in a particular direction, the animation freezes on the very last frame called. I want continous animation flow as I hold onto a key. Very similar to this posters request - viewtopic.php?f=4&t=24768, but using the anim8 lib. The difference here is that I'm guessing love.keyboard.isDown IS checking for a boolean value? Here's my code:

Code: Select all

local anim8 = require('lib.anim8.anim8')
local player, animation, g

function love.load()
  player = {}
  player.spritesheet = love.graphics.newImage('sprites/hero.png')
  player. x = 200
  player.y = 200
  player.speed = 50
        
  g = anim8.newGrid(16, 24, player.spritesheet:getWidth(), player.spritesheet:getHeight())
  animation = anim8.newAnimation('loop', g('1-8,1-5'), 1.0)
end

function love.update(dt)
  if love.keyboard.isDown("w") then 
    player.y = player.y - player.speed * dt
    animation = anim8.newAnimation('loop', g('4-6,1'), 1.0) 
  elseif love.keyboard.isDown("s") then 
    player.y = player.y + player.speed * dt
    animation = anim8.newAnimation('loop', g('1-3,1'), 1.0) 
  elseif love.keyboard.isDown("a") and player.x > 0 then 
    player.x = player.x - player.speed * dt
    animation = anim8.newAnimation('loop', g('7-8,1'), 1.0) 
  elseif love.keyboard.isDown("d") and player.x < 10000 then
    player.x = player.x + player.speed * dt
    animation = anim8.newAnimation('loop', g('4,2'), 1.0)   
  end

  animation:update(dt)
end

function love.draw()
  animation:draw(player.spritesheet, player.x, player.y)
end

function love.keypressed(key) 
   if love.keyboard.isDown("escape") then
     love.event.quit("quit")
    end
end

"Imagination is more important that knowledge...." - Einstein
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Animation while moving sprite?

Post by kikito »

You are creating animations on each frame. Instead of doing that, you should create them once (in love.load, for example) and then reuse them inside love.update.

Example (warning, untested code):

Code: Select all

local anim8 = require('lib.anim8.anim8')
local player

function love.load()
  local spritesheet = love.graphics.newImage('sprites/hero.png');
  local g = anim8.newGrid(16, 24, spritesheet:getWidth(), spritesheet:getHeight())
  
  player = {
    spritesheet = spritesheet,
    x = 200,
    y = 200,
    speed = 50,
    animations = {
      up = anim8.newAnimation('loop', g('4-6,1'), 1.0),
      down = anim8.newAnimation('loop', g('1-3,1'), 1.0),
      left = anim8.newAnimation('loop', g('7-8,1'), 1.0),
      right =  anim8.newAnimation('loop', g('4,2'), 1.0)
    }
  }
  player.animation = player.animations.down -- player starts looking down
end

function love.update(dt)
  if love.keyboard.isDown("w") then 
    player.y = player.y - player.speed * dt
    player.animation = player.animations.up
  elseif love.keyboard.isDown("s") then 
    player.y = player.y + player.speed * dt
    player.animation = player.animations.down
  elseif love.keyboard.isDown("a") and player.x > 0 then 
    player.x = player.x - player.speed * dt
    player.animation = player.animations.left
  elseif love.keyboard.isDown("d") and player.x < 10000 then
    player.x = player.x + player.speed * dt
    player.animation = player.animations.right
  end
  player.animation:update(dt)
end

function love.draw()
  animation:draw(player.spritesheet, player.x, player.y)
end

function love.keypressed(key) 
   if love.keyboard.isDown("escape") then
     love.event.quit("quit")
    end
end
When I write def I mean function.
ghostrunners
Prole
Posts: 8
Joined: Fri Jan 27, 2012 3:40 pm
Location: Georgia

Re: Animation while moving sprite?

Post by ghostrunners »

How terribly exciting that it now works! I only updated animation:draw.. to be player.animation:draw... in love.draw() and viola!

If you'd post the same answer in SO - I'd love to give you points - http://stackoverflow.com/questions/1548 ... g-a-sprite

Thanks!
"Imagination is more important that knowledge...." - Einstein
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Animation while moving sprite?

Post by kikito »

Answered in SO :)
When I write def I mean function.
tilmah
Prole
Posts: 3
Joined: Sun Jun 02, 2013 11:53 pm

Re: Animation while moving sprite?

Post by tilmah »

Hi just a few things in this script i don't understand...
why do you write "...

Code: Select all

spritesheet = spritesheet
.." within the 'player' part, within 'love.load'?
why do you not declare 'spritesheet' as you do 'player' in the first line of code?

i also noticed that you have commas in the wrong order within the different 'animations'..."...

Code: Select all

 up = anim8.newAnimation('loop', g('4-6,1'), 1.0)
...", maybe it should be "...

Code: Select all

up = anim8.newAnimation('loop',g('4-6', 1), 1.0)
..."

thank you for your work on anim8.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Animation while moving sprite?

Post by kikito »

Hi there,
why do you write "...

Code: Select all

spritesheet = spritesheet
.." within the 'player' part, within 'love.load'?
This:

Code: Select all

player = {
  spritesheet = spritesheet,
  x = 200,
  y = 200,
  speed = 50
}
Is the same as this (with a small difference that you don't need to know now):

Code: Select all

player = {}
player.spritesheet = spritesheet
player.x = 200
player.y = 200
player.speed = 50
The one above is just easier to read and write, since it has less letters.
i also noticed that you have commas in the wrong...
The syntax for expressing animations has changed since I answered.

* g('4-6,1') is how you did animations in anim8 1.x (when I answered this question)
* g('4-6',1) is how you do animations in anim8 2.x (it's what you should use now)
thank you for your work on anim8.
You are welcome! :)
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests