Playing idle animation

hello i am trying to play the idle animation when the player stop moving how, please this is the code:

 local function play_animation(self, animation)
 	if self.current_animation ~= animation then
 		self.current_animation = animation
 		sprite.play_flipbook("#sprite", animation)
 	end
 end

 function init(self)
 	msg.post(".", "acquire_input_focus")
 	msg.post("camera", "follow")
 	self.follow = true
 	self.current_animation = nil
 end
 
 
 function on_input(self, action_id, action)
 	local pos = go.get_position()
 	local speed = 1
 	
 	if action_id == hash("up")  then
 		pos.y = pos.y + speed
 		msg.post("camera", "follow")
 		play_animation(self, hash("down"))
 	elseif action_id == hash("down") then
 		pos.y = pos.y - speed
 		msg.post("camera", "follow")
 		play_animation(self, hash("down"))
 	elseif action_id == hash("left") then
 		pos.x = pos.x - speed
 		msg.post("camera", "follow")
 		play_animation(self, hash("down"))
 	elseif action_id == hash("right") then
 		pos.x = pos.x + speed
 		msg.post("camera", "follow")
 		play_animation(self, hash("down"))
 	end
 	
 	go.set_position(pos)
 end

Just add another else there for the case when no key is pressed:

else
    play_animation(self, hash("idle"))
2 Likes

There’s a few issues in your code. The primary concern is that you do not have frame rate independent movement. You update the position in on_input without taking into account the duration of a frame (the delta time in update).

I would suggest that you track the direction of movement (ie which keys that are pressed) in on_input and move the player in update (and update animations), taking into account the delta time.

An example of this can be seen here:

2 Likes

ok thanks

not working