Page 1 of 1

anim8 and key events: how to?

Posted: Wed May 02, 2012 11:40 am
by draconar
Hi,
I'm a LÖVE nooby and working on my first game with this tool.

I've searched through the forums, google, etc and couldn't find an example with uses AnAL or Anim8 (I'd prefer anim8) to animate a sprite on response to a key event -I'm using spritesheets which holds most of the art from my game.

I've managed to get the effect I wanted using quads and image, updating the 'current' quad with a table of quads. But my solution is cumbersome and I don't want to spend time tweaking it since anim8 does the job correctly.

So the question is: in order to attach an animation to a key even using anim8 what should I do? Animate has the method :update(dt) which is called where I used to swap quads, but I don't get how may I trigger other animations in there.

Is that even possible or should I used quads 'by hand'?

cheers!

Re: anim8 and key events: how to?

Posted: Wed May 02, 2012 12:46 pm
by Robin
What is your code so far? We might be able to help you more then.

Re: anim8 and key events: how to?

Posted: Wed May 02, 2012 1:15 pm
by draconar
I just basically followed the anim8's example, but I tried to insert a key listener to it:

Code: Select all

local anim8 = require 'anim8'

local image, animation

function love.load()
  image = love.graphics.newImage('path/to/image.png')
  local g = anim8.newGrid(32, 32, image:getWidth(), image:getHeight())
  animation = anim8.newAnimation('loop', 0.1, g('1-8,1'))
end

function love.update(dt)
  if love.keyboard.isDown("right") then
      -- what should I do?
   elseif love.keyboard.isDown("left") then
      -- what should I do?
   end

   if love.keyboard.isDown("down") then
           -- what should I do?
   elseif love.keyboard.isDown("up") then
            -- what should I do?
   end
  animation:update(dt)
end

function love.draw()
  animation:draw(image, 100, 200)
end

Re: anim8 and key events: how to?

Posted: Wed May 02, 2012 2:47 pm
by kikito
The easiest, simplest way to proceed here is creating two global variables named x and y. Make them start at something in love.load (for example, 100 and 100).

Then, change their values in love.update - don't forget to use dt. For example, when the player presses left, make x = x + 60*dt .( 60 means "sixty pixels per second").

Finally, use x and y instead of 100, 100 in love.draw.

Good luck!

Re: anim8 and key events: how to?

Posted: Mon May 21, 2012 11:59 pm
by Devon Peak
Is this in main.lua or player.lua?

Re: anim8 and key events: how to?

Posted: Tue May 22, 2012 7:28 am
by kikito
I would say that x and y should be defined in player.lua, since they are player-related. You will probably want to expose them to main.lua (either by making them global variables or making your player a table and putting them there, so you can do player.x and player.y from main.lua - I recommend this later option)