2D Animation Sprites while walk?

I’m in trouble. New on learning LUA, and trying do make an ‘RPG’ in defold.
Btw, i learned how to do pixel-art animation to this, but it just do not play while the walk, but when idle.
All the animations groups are separeted, including the idle directions.
Heard about go.animate, but still not sure how to use it.

What should I do?

following lua used ::

if self.direction.y > 0 then 
		msg.post('#sean','play_animation',{id = hash('back_walk')}) 
	elseif self.direction.y < 0 then 
		msg.post('#sean','play_animation',{id = hash('front_walk')})
	end
	if self.direction.x > 0 then 
		msg.post('#sean','play_animation', {id = hash('right_walk')})
	elseif self.direction.x < 0 then 
		msg.post('#sean','play_animation',{id = hash('left_walk')})
	end

Hi! I think the Walking Astronaut tutorial will be helpful:

Or this:

1 Like

didn’t understand …
i mean, undertood the logic behind, but when i execute the ‘debug’ tool points to me an error, even it’s working exactly what i wanted
i found where its the error, but … (?)

in “” local anim = hash(‘idle’) “”" doesn’t recognizes any texture, and then stop the walking animation to idle form, in exactly direction

shall i just ignore?

btw thnks for asnwer<3

I’m not entirely sure I understand what problem you are running into. What is the error you are getting? Do you have an animation/image in your atlas that is named “idle”?

I have 4, to all four directions. (and trying to make for all… )
In this case i was trying to use the front-looking, but the error dont recognizes for some reason
Evan removing the others , its the same

actually i’ll remake this part of script. Almost certain i messed up with some abreviation or invalid name …

again, i tnks

It would be the best to copy the error messages and that part of the code here, so we can give you clues or answers in a moment :wink:

2 Likes

…sorry i totally forgot

“”" Unable to play animation ‘idle_front’ from texture ‘/_generated_c28d3bec.texturec’ since it could not be found. “”"

this is the error message, but when i was checking my animations name… i’ve change the order from words… a dumb mistake, i know ( from ‘front_idle’ to ‘idle_front’)

anyway, this don’t fully solve my problem. ( but i can just ignore it, too )

yeah, it’s working as well, but what i need is the character face te direction when stop walking

Happy to hear you managed to solve the problem!

You mean which animation to play in an idle state after walking? Like walk_front → idle_front?

Exactly! I was trying to add ‘else’ and ‘elseif’ yesterday, but all in vain

If you are unable to solve it then I suggest that you share your full player script here so we can take a look.

I tried the ‘on_reload’ and ‘final’ functions, but not sure how use, and doesnt work so i just deleted
the functions with nothing i deleted from message

function init(self)
	msg.post(".", "acquire_input_focus")
	self.speed = 3
	self.direction = vmath.vector3()
	self.current_anim = nil
end  
	
function update(self, dt)
	if vmath.length_sqr(self.direction) > 1 then                  
		self.dir = vmath.normalize(self.direction)
	end
	local new_position = go.get_position()           
	go.set_position(new_position + self.direction * self.speed)  
	
	local anim = hash('front_idle')

	---Animate by direction
	if self.direction.y > 0 then 
		anim = hash('front_walk')
	elseif self.direction.y < 0 then 
		anim = hash('back_walk')
	elseif self.direction.x > 0 then 
		anim = hash('right_walk')
	elseif self.direction.x < 0 then 
		anim = hash('left_walk')
	end 
	
	if anim ~= self.current_anim then
		msg.post("#sean", "play_animation", { id = anim })
		self.current_anim = anim
	end
	self.direction = vmath.vector3()
end

function on_input(self, action_id, action)
	--movement
	if action_id == hash('move_up') then 
		self.direction.y = 1
	elseif action_id == hash('move_down') then 
		self.direction.y = -1
	end 
	if action_id == hash('move_right') then 
		self.direction.x = 1
	elseif action_id == hash('move_left') then 
		self.direction.x = -1
	end 
end

Thank you for sharing the script! So, you need to somehow keep track of the direction you were moving in the last frame and use this information to display the correct idle animation when the player is no longer moving (ie when self.direction.x and y is 0).

Let’s break this up:

Keep track of direction

I suggest that you create a new variable previous_direction in init()

function init(self)
    ...
    self.direction = vmath.vector3()
    self.previous_direction = vmath.vector3()
end

At the end of the update() function you update previous_direction:

function update(self, dt)
    ...
    self.previous_direction = self.direction
    self.direction = vmath.vector3()
end

Use the previous direction to set idle animation if not moving

Let’s modify your update function where you animate based on direction. If self.direction.x and y is 0 we instead use self.previous_direction to play an idle animation:

if self.direction.y > 0 then 
	anim = hash('front_walk')
elseif self.direction.y < 0 then 
	anim = hash('back_walk')
elseif self.direction.x > 0 then 
	anim = hash('right_walk')
elseif self.direction.x < 0 then 
	anim = hash('left_walk')
else
	-- no movement, play idle animation!
	if self.previous_direction.y > 0 then 
		anim = hash('front_idle')
	elseif self.previous_direction.y < 0 then 
		anim = hash('back_idle')
	elseif self.previous_direction.x > 0 then 
		anim = hash('right_idle')
	elseif self.previous_direction.x < 0 then 
		anim = hash('left_idle')
	end
end 
1 Like

helped a lot! but the walk animation doesnt work anymore
think i’ll just ignore and move on

I’m not sure why that is. If you want to figure it out you need to do some debugging. Here’s a manual page about debugging game logic to get you started: