Page 1 of 2
Create a waking animation through pictures and keypresses
Posted: Mon Feb 20, 2012 10:06 am
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.
Re: Create a waking animation through pictures and keypresse
Posted: Mon Feb 20, 2012 10:51 am
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
Re: Create a waking animation through pictures and keypresse
Posted: Mon Feb 20, 2012 11:14 am
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
Re: Create a waking animation through pictures and keypresse
Posted: Mon Feb 20, 2012 11:24 am
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
Re: Create a waking animation through pictures and keypresse
Posted: Mon Feb 20, 2012 11:26 am
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.
Re: Create a waking animation through pictures and keypresse
Posted: Mon Feb 20, 2012 11:45 am
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
*sigh*
Re: Create a waking animation through pictures and keypresse
Posted: Mon Feb 20, 2012 1:11 pm
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?
Re: Create a waking animation through pictures and keypresse
Posted: Mon Feb 20, 2012 1:25 pm
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.
Re: Create a waking animation through pictures and keypresse
Posted: Mon Feb 20, 2012 1:32 pm
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.
Re: Create a waking animation through pictures and keypresse
Posted: Mon Feb 20, 2012 1:44 pm
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.