Create a waking animation through pictures and keypresses

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.
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

Create a waking animation through pictures and keypresses

Post by Prof_Ants »

So I was wondering how to change an image of character I've created depending on the key I've pressed?
I've got a firm grasp on the if love.keyboard.isDown("left") then x = x - (speed*dt) and I've defined my hero in the love.draw() function with love.graphics.draw(hero, x, y) but at the moment no matter which direction I send my "hero" his image stays the same (of course I've only defined one image at present).

My Ultimate going to to have a walking animation occuring when "right" is pressed but then he stands still when the "right" key is not pressed etc for all directions. All images have been created already.
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

Re: Create a waking animation through pictures and keypresse

Post by Prof_Ants »

I gave this a go to no avail:

Code: Select all

function love.load()

    if love.keyboard.isDown("a") then
        hero = love.graphics.newImage("/hero/11.png")
    elseif love.keyboard.isDown("d") then
        hero = love.graphics.newImage("/hero/5.png")
    elseif love.keyboard.isDown("s") then
        hero = love.graphics.newImage("/hero/fstand.png")
    elseif love.keyboard.isDown("w") then
        hero = love.graphics.newImage("/hero/1.png")
    end

function love.draw()

    love.graphics.draw(background)
    love.graphics.draw(hero, x, y)

end
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Create a waking animation through pictures and keypresse

Post by Nixola »

This

Code: Select all

    if love.keyboard.isDown("a") then
        hero = love.graphics.newImage("/hero/11.png")
    elseif love.keyboard.isDown("d") then
        hero = love.graphics.newImage("/hero/5.png")
    elseif love.keyboard.isDown("s") then
        hero = love.graphics.newImage("/hero/fstand.png")
    elseif love.keyboard.isDown("w") then
        hero = love.graphics.newImage("/hero/1.png")
    end
should go into love.update() or love.draw(), because love.load() is called only once, when you open the file
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

Re: Create a waking animation through pictures and keypresse

Post by Prof_Ants »

Well I'm stumped. I've just tried both. I'm a newb (of course lol), so any help will be appreciated
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Create a waking animation through pictures and keypresse

Post by kikito »

Noticed that you asked the same in StackOverflow. I've answered there, but copy-pasting my answer here:

You must understand how LÖVE works. It (very basically) does this:

Code: Select all

love.load()       -- invoke love.load just once, at the beginning
while true do     -- loop that repeats the following "forever" (until game ends)
  love.update(dt) --   call love.update() 
  love.draw()     --   call love.draw()
end
This schema is so frequent that the loop itself has a name - it's called The Game Loop.

Your code does't work because you are using love.load() as if it was part of the game loop, but it isn't. It's called at the beginning, during the first millisecond or so of your program, and never again.

You want to use love.load do load the images, and love.update to change them:

Code: Select all

function love.load()
  heroLeft  = love.graphics.newImage("/hero/11.png")
  heroRight = love.graphics.newImage("/hero/5.png")
  heroDown  = love.graphics.newImage("/hero/fstand.png")
  heroUp    = love.graphics.newImage("/hero/1.png")

  hero = heroLeft -- the player starts looking to the left
end

function love.update(dt)
  if     love.keyboard.isDown("a") then
    hero = heroLeft
  elseif love.keyboard.isDown("d") then
    hero = heroRight
  elseif love.keyboard.isDown("s") then
    hero = heroDown
  elseif love.keyboard.isDown("w") then
    hero = heroUp
  end
end

function love.draw()
  love.graphics.draw(background)
  love.graphics.draw(hero, x, y)
end
The code above has certain repetitiveness that can be factored out using tables, but I've left it simple on purpose.

You will also notice that I have included the dt parameter in the love.update function. This is important, since you will need it to make sure that animations work the same in all computers (the speed at which love.update is called depends on each computer, and dt allows you to cope with that)

Nevertheless, if you want to do animations, you will probably want to use this Animation Lib.
When I write def I mean function.
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

Re: Create a waking animation through pictures and keypresse

Post by Prof_Ants »

Thanks again. The code failed giving a "Incorrect paramater type: expected userdata....in function 'draw'" which makes me think animations should be what I should be looking into anyway. Might as well learn the CORRECT way rather than the wrong way when I'm only going to need to change my habits later.

Edit

I missed the

Code: Select all

 hero = heroLeft
*sigh*
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

Re: Create a waking animation through pictures and keypresse

Post by Prof_Ants »

I hope no one minds me asking loads of questions...

I'm hitting a new brick wall, I'm having trouble assigning animations to the main character. I've been trying to do this:

Code: Select all

function love.load()
    require("AnAL")
    local img  = love.graphics.newImage("/hero/heroanime.png")
    walking_up = newAnimation(img, 71, 100.5, 0.1, 3)

function love.update(dt)
    walking_up:update(dt)
    if love.keyboard.isDown("w") then
		y = y - (speed*dt)
		hero = walking_up
    end
function love.draw()
     love.graphics.draw(hero, x, y)
Can anyone point me in the right direction?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Create a waking animation through pictures and keypresse

Post by tentus »

Three problems. One, you should end functions with the keyword end.

Code: Select all

function love.load()
    require("AnAL")
    local img  = love.graphics.newImage("/hero/heroanime.png")
    walking_up = newAnimation(img, 71, 100.5, 0.1, 3)
end
function love.update(dt)
    walking_up:update(dt)
    if love.keyboard.isDown("w") then
		y = y - (speed*dt)
		hero = walking_up
    end
end
function love.draw()
     love.graphics.draw(hero, x, y)
end
Two, you're missing hero in love.load again. You should also define x, y, and speed in love.load.

Three, I don't think 100.5 is a valid frame height. One hundred and one half pixels? Half pixels are usually problematic.
Kurosuke needs beta testers
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

Re: Create a waking animation through pictures and keypresse

Post by Prof_Ants »

I really should have mentioned they are all in there I was just trying to skim it down and share what parts I thought were relevant. The part I'm unclear on atm is assigning an animation to a direction. All animations function correctly when running independently when I debug.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Create a waking animation through pictures and keypresse

Post by tentus »

Post what you've got and I'll see if I can spot it. It's not a problem to have a long code post.
Kurosuke needs beta testers
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests