Character with 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.
User avatar
More Ragtime
Prole
Posts: 10
Joined: Sat Aug 10, 2013 1:32 am

Character with animation

Post 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.
User avatar
borix134
Prole
Posts: 18
Joined: Sat Aug 10, 2013 5:37 am

Re: Character with animation

Post by borix134 »

Well, my only guess would be to try the code without 'bullet' as a local variable.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Character with animation

Post 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.
Help us help you: attach a .love.
User avatar
More Ragtime
Prole
Posts: 10
Joined: Sat Aug 10, 2013 1:32 am

Re: Character with animation

Post 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.
User avatar
borix134
Prole
Posts: 18
Joined: Sat Aug 10, 2013 5:37 am

Re: Character with animation

Post 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
User avatar
More Ragtime
Prole
Posts: 10
Joined: Sat Aug 10, 2013 1:32 am

Re: Character with animation

Post 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'
User avatar
borix134
Prole
Posts: 18
Joined: Sat Aug 10, 2013 5:37 am

Re: Character with animation

Post 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
User avatar
More Ragtime
Prole
Posts: 10
Joined: Sat Aug 10, 2013 1:32 am

Re: Character with animation

Post 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'
User avatar
XxSolidJelloxX
Prole
Posts: 25
Joined: Sun Jul 14, 2013 2:46 am
Location: The Stars

Re: Character with animation

Post 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
User avatar
borix134
Prole
Posts: 18
Joined: Sat Aug 10, 2013 5:37 am

Re: Character with animation

Post 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
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests