anim8 and key events: how to?

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
draconar
Prole
Posts: 2
Joined: Wed May 02, 2012 11:33 am

anim8 and key events: how to?

Post 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!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: anim8 and key events: how to?

Post by Robin »

What is your code so far? We might be able to help you more then.
Help us help you: attach a .love.
draconar
Prole
Posts: 2
Joined: Wed May 02, 2012 11:33 am

Re: anim8 and key events: how to?

Post 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
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: anim8 and key events: how to?

Post 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!
When I write def I mean function.
Devon Peak
Prole
Posts: 7
Joined: Sat May 19, 2012 5:42 pm
Location: Streetsboro, OH
Contact:

Re: anim8 and key events: how to?

Post by Devon Peak »

Is this in main.lua or player.lua?
Aspiring fine artist, graphic designer, and game developer.
website: http://www.devonpeak.wordpress.com
twitter: http://www.twitter.com/_devonpeak
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: anim8 and key events: how to?

Post 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)
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests