Instead of thinking about playing the animations in sequence, have you considered modeling the states of the character? The code below is just an example of the idea (not tested), but it’s a very useful pattern.
local STATE_IDLE = "IDLE"
local STATE_ATTACK = "ATTACK"
local STATE_WALK = "WALK"
local state = STATE_IDLE
function set_state_idle()
state = STATE_IDLE
sprite.play_flipbook("#sprite", "idle")
end
function set_state_walk(direction)
state = STATE_WALK
sprite.play_flipbook("#sprite", "walk")
sprite.set_hflip(direction < 0)
end
function set_state_attack()
state = STATE_ATTACK
sprite.play_flipbook("#sprite", "attack", function()
-- go to idle. could also have conditional logic to go to walking
set_state_idle()
end)
end
function on_input(self, action_id, action)
if action_id == hash("right") and action.pressed then
set_state_walk(1)
elseif action_id == hash("left") and action.pressed then
set_state_walk(-1)
elseif action_id == hash("right") or action_id == hash("left") and action.released then
set_state_idle()
elseif action_id == hash("touch") and action.pressed then
set_state_attack()
end
end