Ok, so with the logic you have you’ll play the idle animation as soon as any key is released. If you want to support multiple keys pressed at the same time, for instance pressing left+right at the same time and only play the idle animation when both are released you need to track that. Something like this:
local LEFT = hash("left")
local RIGHT = hash("right")
function init(self)
-- we store input action pressed/released state here, keyed on action_id
self.actions = {}
end
function on_input(self, action_id, action)
if action.pressed then
-- set pressed state for action_id to true
self.actions[action_id] = true
-- set direction, sprite flip and walk animation
if action_id == RIGHT then
self.direction.x = 8
sprite.set_hflip("#Sprite", false)
sprite.play_animation("#Sprite", "walk")
elseif action_id == LEFT then
self.direction.x = -8
sprite.set_hflip("#Sprite", true)
sprite.play_animation("#Sprite", "walk")
end
elseif action.released then
-- clear pressed state for action_id
self.actions[action_id] = false
-- switch to idle animation if neither "left" or "right" is pressed
if not self.actions[LEFT] and not self.actions[RIGHT] then
sprite.play_animation("#Sprite", "idle")
end
end
end