Page 1 of 1

Animating

Posted: Sun May 20, 2012 11:01 am
by Kasperelo
Please help me! I'm not sure what's wrong with the animation.

Re: Animating

Posted: Sun May 20, 2012 11:28 am
by IMP1
I think it may be the line(s) in 'player.lua':

Code: Select all

player.iterator = player.iterator + dt
That's not adding a whole number, but player.iterator (I think) has to be an integer. Try adding 'love.graphics.print(player.iterator, 0, 0) to your draw function so you can see what the variable is.

Add another varaible, call it animationTimer or something, and add dt to it every frame. Then if animationTimer >= [however long you wait between animation frames], then add one to player.iterator, and subtract dt from animationTimer.

Or use an animation library/class. I'm sure there are lots out there.

Re: Animating

Posted: Sun May 20, 2012 11:33 am
by Kasperelo
OK!
I'll try

Re: Animating

Posted: Sun May 20, 2012 11:42 am
by Kasperelo
Hm. Kinda works, but for some reason one of the frames doesn't show up?

Re: Animating

Posted: Sun May 20, 2012 11:50 am
by juno
You're trying to access a table value (playerQuads.down, playerQuads.up..) with a string value.
One way to solve this is to create an empty table - playerQuads = { }, create the 4 quad animations as separate tables and then add them to playerQuads so that playerQuads[1] is down, playerQuads[2] is up etc. This means you need to change your player.direction value to an integer 1-4 instead of strings.

Here, I made a few changes to your player

Code: Select all

	control = {
		up = "w",
		down = "s",
		left = "a",
		right = "d",
		attack = "l"
	}
	
	player = {
		x = 600,
		y = 325,
		speed = 200,
		sprite = love.graphics.newImage("pics/quads/player.png"),
		direction = 1,
		iterator = 1
	}
	
	local playerQuads={}
	
	local downQuad = {
			love.graphics.newQuad(0, 0, 24, 63, 96, 252),
			love.graphics.newQuad(0, 63, 24, 63, 96, 252),
			love.graphics.newQuad(0, 126, 24, 63, 96, 252),
			love.graphics.newQuad(0, 189, 24, 63, 96, 252)
		}
	local upQuad = {
		love.graphics.newQuad(24, 0, 24, 63, 96, 252),
		love.graphics.newQuad(24, 63, 24, 63, 96, 252),
		love.graphics.newQuad(24, 126, 24, 63, 96, 252),
		love.graphics.newQuad(24, 189, 24, 63, 96, 252)
	} 
	local rightQuad = {
		love.graphics.newQuad(48, 0, 24, 63, 96, 252),
			love.graphics.newQuad(48, 63, 24, 63, 96, 252),
			love.graphics.newQuad(48, 126, 24, 63, 96, 252),
			love.graphics.newQuad(48, 189, 24, 63, 96, 252)
	} 
	local leftQuad = {
		love.graphics.newQuad(72, 0, 24, 63, 96, 252),
			love.graphics.newQuad(72, 63, 24, 63, 96, 252),
			love.graphics.newQuad(72, 126, 24, 63, 96, 252),
			love.graphics.newQuad(72, 189, 24, 63, 96, 252)
	} 

table.insert(playerQuads, downQuad)--1
table.insert(playerQuads, upQuad)--2
table.insert(playerQuads, rightQuad)--3
table.insert(playerQuads, leftQuad)--4

function playerDraw()

	if gamestate == "playing" then
		love.graphics.drawq(player.sprite, playerQuads[player.direction][player.iterator], player.x, player.y)
	end
	
end

function playerMove(dt)
	
	if love.keyboard.isDown(control.up) then
		player.y = player.y - player.speed * dt
		player.direction = 2
		player.iterator = player.iterator + 1
	end
	
	if love.keyboard.isDown(control.down) then
		player.y = player.y + player.speed * dt
		player.direction = 1
		player.iterator = player.iterator + 1
	end

	if love.keyboard.isDown(control.right) then
		player.x = player.x + player.speed * dt
		player.direction = 3
		player.iterator = player.iterator + 1
	end

	if love.keyboard.isDown(control.left) then
		player.x = player.x - player.speed * dt
		player.direction = 4
		player.iterator = player.iterator + 1
	end

end

function playerTimer(dt)

	if player.iterator > 4 then
		player.iterator = 1
	end

end


Re: Animating

Posted: Sun May 20, 2012 11:52 am
by Kasperelo
uh... It worked. Didn't need to make an empty table. Weird. But now, if you walk around a little he's gonna dance like an idiot.

Re: Animating

Posted: Sun May 20, 2012 1:37 pm
by Kasperelo
Fixed! Yeah!

Re: Animating

Posted: Fri May 25, 2012 1:56 am
by Showle1943
Kasperelo wrote:Hm. Kinda works, but for some reason one of the frames doesn't show up?
I'm having the same issue. Anybody figure this out yet? I'm a newbie so be gentle. :3

Re: Animating

Posted: Fri May 25, 2012 7:40 pm
by Kasperelo
Showle1943 wrote:
Kasperelo wrote:Hm. Kinda works, but for some reason one of the frames doesn't show up?
I'm having the same issue. Anybody figure this out yet? I'm a newbie so be gentle. :3
Fixed it! Here's the script. PM me what you don't understand, if there's something unclear.