[SOLVED]Giving the user ability to move an 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.
Post Reply
User avatar
hrobohboy
Prole
Posts: 4
Joined: Tue Jul 07, 2015 7:19 pm
Location: 'Murica

[SOLVED]Giving the user ability to move an animation

Post by hrobohboy »

Hi! So I'm kind of new here, but I tried searching the forum (literally for the last two hours, reading all relevant posts) and found nothing that could particularly help me.
I am currently playing around with Lua and love (using the ZeroBrane IDE) and I have managed to make my little pac-man thing move its mouth; however, I don't seem to be having any luck making it move using the "wasd" keys. I will attach a zip file with the main.lua and the pngs, but here is the bulk of the code:

Code: Select all

function love.load()
  animation = {}
  frame = 1
  counter = 0
  love.graphics.setBackgroundColor({255, 255, 255})
  animation.x = 300
  animation.y = 300
  animation.speed = 100
  animation[1] = love.graphics.newImage("first.png")
  animation[2] = love.graphics.newImage("second.png")
end
function love.update(dt)
  counter = counter + dt
  if counter >=0.2 then
    counter = 0
    frame = frame +1
    if frame == 3 then
      frame = 1
    end
  end
  if love.keyboard.isDown("a") then
      animation.x = animation.x - animation.speed*dt
  elseif love.keyboard.isDown("s") then
      animation.y = animation.y + animation.speed*dt
  elseif love.keyboard.isDown("d") then
      animation.x = animation.x + animation.speed*dt
  elseif love.keyboard.isDown("w") then
      animation.y = animation.y - animation.speed*dt
end
end

function love.draw()
  love.graphics.draw(animation[frame], 0, 0)
end
I also know that there are simpler solutions to the whole animation thing, but I wanted to hard code it first, just to figure out how it really works. I really just want to understand why I can't get the stupid thing to move! I figure maybe I was doing something wrong with the animation table, but now I'm just kinda lost. Sorry for the weird naming as well, I'm going to end up using that folder for a bigger project once I get a better hold of Lua/Love. Any help is greatly appreciated! :D
Attachments
gunslinger_game-2015-07-07.zip
(3.22 KiB) Downloaded 115 times
Last edited by hrobohboy on Thu Jul 09, 2015 3:15 am, edited 1 time in total.
I am cursed to trek through the Desert of Life, searching for an Oasis that does not exist. :emo:
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Giving the user ability to move an animation

Post by DaedalusYoung »

In your love.draw function, you're placing the animation sprite always at coordinates 0, 0. You must of course set this to animation.x and animation.y for it to put the image at those coordinates instead.
User avatar
hrobohboy
Prole
Posts: 4
Joined: Tue Jul 07, 2015 7:19 pm
Location: 'Murica

Re: Giving the user ability to move an animation

Post by hrobohboy »

I knew I was missing something simple! Thanks a bunch!
I am cursed to trek through the Desert of Life, searching for an Oasis that does not exist. :emo:
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests