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

Re: Character with animation

Post by More Ragtime »

DaedalusYoung 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
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 move
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: Character with animation

Post by DaedalusYoung »

Because speed has no value. Add it in love.load(), for example like so:

Code: Select all

speed = 100
Otherwise, it will have no value (it will be nil). How would you calculate no value * 1? You can't, and neither can Lua.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Character with animation

Post by Ranguna259 »

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

Code: Select all

love.graphics.draw (bullet, x, y)
to:

Code: Select all

bullet:drawAnimation(x,y)
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: Character with animation

Post by DaedalusYoung »

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.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Character with animation

Post by Ranguna259 »

But why, AnAL already exists and it perfect
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: Character with animation

Post by DaedalusYoung »

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.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Character with animation

Post by Ranguna259 »

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

Code: Select all

anim:drawAnimation(x,y)
Whenever you want to draw an animation with AnAL you do

Code: Select all

animation:drawAnimation(x,y)
and never forget to do the

Code: Select all

animation:update(dt)
on love.update(dt)
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
XxSolidJelloxX
Prole
Posts: 25
Joined: Sun Jul 14, 2013 2:46 am
Location: The Stars

Re: Character with animation

Post by XxSolidJelloxX »

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:

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

Who is online

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