Page 1 of 3
Character with animation
Posted: Sat Aug 10, 2013 1:40 am
by More Ragtime
Hi, im trying to code a charcter that has a walking animation. I'm using the hampster ball tutorial with the AnAL tutorial and it seems to be conflicting, but i don't know of any other way to put an idle or walking animation in the game
this is my main.lua code
Code: Select all
require("AnAL")
function love.load()
local bullet = love.graphics.newImage("bullet.png")
anim = newAnimation(bullet, 128,128, 0.1, 0)
x = 50
y = 50
speed = 100
anim:setMode("loop")
end
function love.update(dt)
-- Updates the animation. (Enables frame changes)
anim:update(dt)
if love.keyboard.isDown("d") then
x = x + (speed * dt)
elseif love.keyboard.isDown("a") then
x = x - (speed * dt)
end
if love.keyboard.isDown("s") then
y = y + (speed * dt)
elseif love.keyboard.isDown("w") then
y = y - (speed * dt)
end
end
function love.draw()
love.graphics.draw (bullet, x, y)
end
and the error im getting is
Code: Select all
error
main.lua:30: Incorrect parameter type: expected userdata.
traceback
[C]: in fucntion 'draw'
main.lua:30: in fucntion 'draw'
[C]: in function 'xpcall'
thank you for your time.
Re: Character with animation
Posted: Sat Aug 10, 2013 6:10 am
by borix134
Well, my only guess would be to try the code without 'bullet' as a local variable.
Re: Character with animation
Posted: Sat Aug 10, 2013 7:37 am
by Robin
borix134 is correct: the variable bullet is local to love.load, so it's not accessible in love.draw. Just remove the keyword local.
Re: Character with animation
Posted: Sat Aug 10, 2013 2:46 pm
by More Ragtime
When i did that the animation didn't play it was just the whole image being drawn. The character moves though. is AnAL the only practical way to play animations in love? i though i heard they cut it from love.
Re: Character with animation
Posted: Sat Aug 10, 2013 3:07 pm
by borix134
Well, you could try something like this:
Code: Select all
function love.load()
animation = {}
animation.frame = 1
animation.frame1 = (some image)
animation.frame2 = (some other image)
animation.frame3 = (yet another image)
animation.image = animation.frame1
end
function love.update(dt)
if animation.frame ==1 then
animation.image = animation.frame1
frame=frame + 1
else if animation.frame == 2 then
frame = frame + 1
animation.image = animation.frame2
else if animation.frame == 3 then
animation.image = animation.frame3
animation.frame = 1
end
end
function love.draw()
love.graphics.draw(animation.image,0,0)
end
Re: Character with animation
Posted: Sat Aug 10, 2013 3:28 pm
by More Ragtime
Code: Select all
unction love.load()
animation = {}
animation.frame = 1
animation.frame1 = (untitled)
animation.frame2 = (untitled1)
animation.frame3 = (untitled2)
animation.image = animation.frame1
end
function love.update(dt)
if animation.frame ==1 then
animation.image = animation.frame1
frame=frame + 1
else if animation.frame == 2 then
frame = frame + 1
animation.image = animation.frame2
else if animation.frame == 3 then
animation.image = animation.frame3
animation.frame = 1
end
end
function love.draw()
love.graphics.draw(animation.image,0,0)
end
I'm getting a syntax error with that
Code: Select all
Error
Syntax error: main.lua:25: 'end' ex[ected (to close 'if' at line 11) near '<eof>'
traceback
[c]: ?
[C]: in function 'require'
[C]: in function 'xpcall'
Re: Character with animation
Posted: Sat Aug 10, 2013 3:38 pm
by borix134
My bad, as it turns out, there is no space between else and if. Updated code:
Code: Select all
function love.load()
animation = {}
animation.frame = 1
animation.frame1 = (untitled)
animation.frame2 = (untitled1)
animation.frame3 = (untitled2)
animation.image = animation.frame1
end
function love.update(dt)
if animation.frame ==1 then
animation.image = animation.frame1
frame=frame + 1
elseif animation.frame == 2 then
frame = frame + 1
animation.image = animation.frame2
elseif animation.frame == 3 then
animation.image = animation.frame3
animation.frame = 1
end
end
function love.draw()
love.graphics.draw(animation.image,0,0)
end
Re: Character with animation
Posted: Sat Aug 10, 2013 5:05 pm
by More Ragtime
another error
Code: Select all
Error
main.lua:12: attempt to perform arithmetic on global 'frame' (a nil value)
Traceback
main.lua:12: in function 'update'
[C]:in function 'xpcall'
Re: Character with animation
Posted: Sat Aug 10, 2013 5:32 pm
by XxSolidJelloxX
Try this:
Code: Select all
function love.load()
animation = {}
frame = 0
animation.frame = 1
animation.frame1 = (untitled)
animation.frame2 = (untitled1)
animation.frame3 = (untitled2)
animation.image = animation.frame1
end
function love.update(dt)
if animation.frame ==1 then
animation.image = animation.frame1
frame=frame + 1
elseif animation.frame == 2 then
frame = frame + 1
animation.image = animation.frame2
elseif animation.frame == 3 then
animation.image = animation.frame3
animation.frame = 1
end
end
function love.draw()
love.graphics.draw(animation.image,0,0)
end
Re: Character with animation
Posted: Sat Aug 10, 2013 5:39 pm
by borix134
@solidjello If your gonna do that, then you need to remove the animation.frame variable, like so:
Code: Select all
function love.load()
animation = {}
frame = 0
animation.frame0 = (untitled)
animation.frame1 = (untitled1)
animation.frame2 = (untitled2)
animation.image = animation.frame1
end
function love.update(dt)
if frame ==0 then
animation.image = animation.frame0
frame=frame + 1
elseif frame == 1 then
frame = frame + 1
animation.image = animation.frame1
elseif frame == 2 then
animation.image = animation.frame2
frame = 0
end
end
function love.draw()
love.graphics.draw(animation.image,0,0)
end