thanks, it worked. but trying to move doesn't quite work. its telling me "attempt to preform arithmatic on global 'speed' (nil value) when i press any of the wasd keys to moveDaedalusYoung wrote:Because you're attempting to feed a table into love.graphics.draw().
Try this:
Code: Select all
function love.load() animation = {} animation[1] = love.graphics.newImage('Untitled.png') animation[2] = love.graphics.newImage('Untitled2.png') animation[3] = love.graphics.newImage('Untitled3.png') counter = 0 frame = 1 end function love.update(dt) counter = counter + dt if counter >= 0.2 then counter = 0 frame = frame + 1 if frame == 4 then frame = 1 end end 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(animation[frame], 0, 0) end
Character with animation
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- More Ragtime
- Prole
- Posts: 10
- Joined: Sat Aug 10, 2013 1:32 am
Re: Character with animation
- DaedalusYoung
- Party member
- Posts: 407
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Character with animation
Because speed has no value. Add it in love.load(), for example like so:
Otherwise, it will have no value (it will be nil). How would you calculate no value * 1? You can't, and neither can Lua.
Code: Select all
speed = 100
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
Re: Character with animation
Wha..? No, just NO.
Building an animation code yourself is a pain in the ass and it'll just mess up your code even if you make another lua to hold the animation code you'll just think back on how much time you used just to create something that already exists and that there was no real need to recreate it.
All you need to do is to change your line 30 from
to:
Building an animation code yourself is a pain in the ass and it'll just mess up your code even if you make another lua to hold the animation code you'll just think back on how much time you used just to create something that already exists and that there was no real need to recreate it.
All you need to do is to change your line 30 from
Code: Select all
love.graphics.draw (bullet, x, y)
Code: Select all
bullet:drawAnimation(x,y)
- DaedalusYoung
- Party member
- Posts: 407
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Character with animation
I see no problem in creating one's own animation code. All you do is change exactly which image you draw to the screen over time. Time is easy to keep track of, and based on that, it's easy to alternate the image.
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
Re: Character with animation
But why, AnAL already exists and it perfect
- DaedalusYoung
- Party member
- Posts: 407
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Character with animation
It can be a good exercise in coding, and gives you a better idea how animations work. When I made my first project, I wrote my own animation code and it's fine. AnAL probably is too powerful for what I needed and would only have made the .love file bigger.
Also, I like using my own coding 'conventions', the library is most likely coded in a completely different way than how I code.
For bigger projects, AnAL is probably better, if you need all or most of its functionalities.
Also, I like using my own coding 'conventions', the library is most likely coded in a completely different way than how I code.
For bigger projects, AnAL is probably better, if you need all or most of its functionalities.
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
Re: Character with animation
Yeah, I guess it's good to exercise
But Ragtime all you need to do to fix you code is to change line 30 to
Whenever you want to draw an animation with AnAL you do and never forget to do the on love.update(dt)
But Ragtime all you need to do to fix you code is to change line 30 to
Code: Select all
anim:drawAnimation(x,y)
Code: Select all
animation:drawAnimation(x,y)
Code: Select all
animation:update(dt)
- XxSolidJelloxX
- Prole
- Posts: 25
- Joined: Sun Jul 14, 2013 2:46 am
- Location: The Stars
Re: Character with animation
I've come up with what I think Is a better way to do this. Keep in mind that I am pretty new to love. Let me know if this way is efficient:
This is designed for only 2 walking frames (not including the one where the character is standing still), but It could be easily tweaked to be used for more. Also, at the moment you should only put in the frames of the character walking right, because that is the only control I have programmed just as the example, but again, this could be easily tweaked to make all directions work. Anyways, let me know what you guys think
Edit: I did it with all directions, and it lags for some reason. Looks like I'll be using AnAL.
Code: Select all
function love.load()
animation = {}
animation[1] = love.graphics.newImage('FirstPartOf-Walking-Animation.png')
animation[2] = love.graphics.newImage('SecondPartOf-Walking-Animation.png')
animation[3] = love.graphics.newImage('Character-Standing-Still.png')
frame = 3
charX = 0
charY = 0
rightTicks = 0
end
function love.update(dt)
print (rightTicks)
if rightTicks == 12 then
rightTicks = 0
frame = frame + 1
end
if moveRight then
rightTicks = rightTicks + 1
charX = charX + 2
if frame == 3 then
frame = 1
end
end
end
function love.draw()
love.graphics.draw(animation[frame], charX, charY)
end
function love.keypressed( key, unicode )
if key == "d" then
moveRight = true
end
end
function love.keyreleased( key, unicode )
if key == "d" then
moveRight = false
rightTicks = 0
frame = 3
end
end
Edit: I did it with all directions, and it lags for some reason. Looks like I'll be using AnAL.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests