Page 1 of 1

helps animation to walk in a rpg game. - (Solved)

Posted: Sun Sep 01, 2013 3:54 am
by luislasonbra
hello as I can make a walking animation pokemon style with love2d
I have the following file using an animation, but not it's what I want.
Player Tile Animation.love
(20.32 KiB) Downloaded 299 times
please help.

Re: helps animation to walk in a rpg game.

Posted: Sun Sep 01, 2013 9:46 am
by chezrom
The first problem I see is the love.timer.sleep in the player.update function. You don't need that and it kills your frame rate because the main loop (the love.run function) call love.update and then the love.draw function.
The second problem is that you add your speed (32), so the sprite teleport around. If you want a progressive walk, use speed*dt to modify your player.x
Another problem is that the first thing you do when a key is down is to restart the walk cycle. (sorry for my syntax)

I don't know what you want so I suppose you don't want a grid locked char, so here is an example of player.lua with progressive moving :

I use your player.pnum as picture num and player.pic to indicate the pictures' table you want to display.
wpic in function player.update is the wanted picture table.
I use player.speed as the horizontal speed of the player (negative to left, positive to right).
I increase player.speed but ANIME_TIMER and player.speed need some tuning for a good feeling.
The main change are in the player.update function.

Code: Select all

player = {}

player.x = 100
player.y = 100
player.speed = 64
local ANIM_TIMER=0.25
playerL = {}
playerL[1] = love.graphics.newImage("image/left_00.png")
playerL[2] = love.graphics.newImage("image/left_01.png")
playerL[3] = love.graphics.newImage("image/left_02.png")
playerL[4] = love.graphics.newImage("image/left_03.png")
playerL[5] = love.graphics.newImage("image/left_04.png")
playerL[6] = love.graphics.newImage("image/left_05.png")
playerL[7] = love.graphics.newImage("image/left_06.png")

playerR = {}
playerR[1] = love.graphics.newImage("image/right_00.png")
playerR[2] = love.graphics.newImage("image/right_01.png")
playerR[3] = love.graphics.newImage("image/right_02.png")
playerR[4] = love.graphics.newImage("image/right_03.png")
playerR[5] = love.graphics.newImage("image/right_04.png")
playerR[6] = love.graphics.newImage("image/right_05.png")
playerR[7] = love.graphics.newImage("image/right_06.png")

player.animTimer = 0
player.pic = playerR
player.pnum = 1

function player.update(dt)
    if love.keyboard.isDown("right","left") then
		local wpic = love.keyboard.isDown("left") and playerL or playerR
		if player.pic ~= wpic then
			player.pic = wpic	
			player.pnum=1
			player.animTimer=ANIM_TIMER
			player.speed = -player.speed
		end
		player.animTimer = player.animTimer - dt
		if player.animTimer < 0 then
			player.animTimer=ANIM_TIMER
			player.pnum = player.pnum +1
			if player.pnum > #player.pic then
				player.pnum=1
			end
		end
		player.x = player.x + player.speed*dt
    else
		player.pnum=1
	end
end

function player.draw()
    love.graphics.draw(player.pic[player.pnum], player.x, player.y)
end


Re: helps animation to walk in a rpg game.

Posted: Sun Sep 01, 2013 4:53 pm
by luislasonbra
chezrom wrote:The first problem I see is the love.timer.sleep in the player.update function. You don't need that and it kills your frame rate because the main loop (the love.run function) call love.update and then the love.draw function.
The second problem is that you add your speed (32), so the sprite teleport around. If you want a progressive walk, use speed*dt to modify your player.x
Another problem is that the first thing you do when a key is down is to restart the walk cycle. (sorry for my syntax)

I don't know what you want so I suppose you don't want a grid locked char, so here is an example of player.lua with progressive moving :

I use your player.pnum as picture num and player.pic to indicate the pictures' table you want to display.
wpic in function player.update is the wanted picture table.
I use player.speed as the horizontal speed of the player (negative to left, positive to right).
I increase player.speed but ANIME_TIMER and player.speed need some tuning for a good feeling.
The main change are in the player.update function.

Code: Select all

player = {}

player.x = 100
player.y = 100
player.speed = 64
local ANIM_TIMER=0.25
playerL = {}
playerL[1] = love.graphics.newImage("image/left_00.png")
playerL[2] = love.graphics.newImage("image/left_01.png")
playerL[3] = love.graphics.newImage("image/left_02.png")
playerL[4] = love.graphics.newImage("image/left_03.png")
playerL[5] = love.graphics.newImage("image/left_04.png")
playerL[6] = love.graphics.newImage("image/left_05.png")
playerL[7] = love.graphics.newImage("image/left_06.png")

playerR = {}
playerR[1] = love.graphics.newImage("image/right_00.png")
playerR[2] = love.graphics.newImage("image/right_01.png")
playerR[3] = love.graphics.newImage("image/right_02.png")
playerR[4] = love.graphics.newImage("image/right_03.png")
playerR[5] = love.graphics.newImage("image/right_04.png")
playerR[6] = love.graphics.newImage("image/right_05.png")
playerR[7] = love.graphics.newImage("image/right_06.png")

player.animTimer = 0
player.pic = playerR
player.pnum = 1

function player.update(dt)
    if love.keyboard.isDown("right","left") then
		local wpic = love.keyboard.isDown("left") and playerL or playerR
		if player.pic ~= wpic then
			player.pic = wpic	
			player.pnum=1
			player.animTimer=ANIM_TIMER
			player.speed = -player.speed
		end
		player.animTimer = player.animTimer - dt
		if player.animTimer < 0 then
			player.animTimer=ANIM_TIMER
			player.pnum = player.pnum +1
			if player.pnum > #player.pic then
				player.pnum=1
			end
		end
		player.x = player.x + player.speed*dt
    else
		player.pnum=1
	end
end

function player.draw()
    love.graphics.draw(player.pic[player.pnum], player.x, player.y)
end


hello the animation I want to achieve is this
pkmn (1).love
(213.75 KiB) Downloaded 161 times
but I can not get the code for the animation.

Re: helps animation to walk in a rpg game.

Posted: Mon Sep 02, 2013 10:51 am
by luislasonbra
luislasonbra wrote:
chezrom wrote:The first problem I see is the love.timer.sleep in the player.update function. You don't need that and it kills your frame rate because the main loop (the love.run function) call love.update and then the love.draw function.
The second problem is that you add your speed (32), so the sprite teleport around. If you want a progressive walk, use speed*dt to modify your player.x
Another problem is that the first thing you do when a key is down is to restart the walk cycle. (sorry for my syntax)

I don't know what you want so I suppose you don't want a grid locked char, so here is an example of player.lua with progressive moving :

I use your player.pnum as picture num and player.pic to indicate the pictures' table you want to display.
wpic in function player.update is the wanted picture table.
I use player.speed as the horizontal speed of the player (negative to left, positive to right).
I increase player.speed but ANIME_TIMER and player.speed need some tuning for a good feeling.
The main change are in the player.update function.

Code: Select all

player = {}

player.x = 100
player.y = 100
player.speed = 64
local ANIM_TIMER=0.25
playerL = {}
playerL[1] = love.graphics.newImage("image/left_00.png")
playerL[2] = love.graphics.newImage("image/left_01.png")
playerL[3] = love.graphics.newImage("image/left_02.png")
playerL[4] = love.graphics.newImage("image/left_03.png")
playerL[5] = love.graphics.newImage("image/left_04.png")
playerL[6] = love.graphics.newImage("image/left_05.png")
playerL[7] = love.graphics.newImage("image/left_06.png")

playerR = {}
playerR[1] = love.graphics.newImage("image/right_00.png")
playerR[2] = love.graphics.newImage("image/right_01.png")
playerR[3] = love.graphics.newImage("image/right_02.png")
playerR[4] = love.graphics.newImage("image/right_03.png")
playerR[5] = love.graphics.newImage("image/right_04.png")
playerR[6] = love.graphics.newImage("image/right_05.png")
playerR[7] = love.graphics.newImage("image/right_06.png")

player.animTimer = 0
player.pic = playerR
player.pnum = 1

function player.update(dt)
    if love.keyboard.isDown("right","left") then
		local wpic = love.keyboard.isDown("left") and playerL or playerR
		if player.pic ~= wpic then
			player.pic = wpic	
			player.pnum=1
			player.animTimer=ANIM_TIMER
			player.speed = -player.speed
		end
		player.animTimer = player.animTimer - dt
		if player.animTimer < 0 then
			player.animTimer=ANIM_TIMER
			player.pnum = player.pnum +1
			if player.pnum > #player.pic then
				player.pnum=1
			end
		end
		player.x = player.x + player.speed*dt
    else
		player.pnum=1
	end
end

function player.draw()
    love.graphics.draw(player.pic[player.pnum], player.x, player.y)
end


hello the animation I want to achieve is this
The attachment pkmn (1).love is no longer available
but I can not get the code for the animation.

hi thanks I've managed to make the animations.

here I leave the code in case anyone is interested.
Player Tile Animation v0.3.love
(38.96 KiB) Downloaded 190 times