direction needed to pause animatedsprite on certain frame when input is absent?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pinklemonad3

i have a few animations attached to my controls so far, as seen in the code below:
if direction.x != 0:
$AnimatedSprite.animation = “left_right”
$AnimatedSprite.flip_v = false
$AnimatedSprite.flip_h = direction.x < 0
elif direction.y < 0:
$AnimatedSprite.animation = “up”
elif direction.y > 0:
$AnimatedSprite.animation = “down”
each of these animations uses a few frames. i was planning on setting the idle state to one of the frames in the animations, but i’m not sure how. i don’t know if i would use cords like i did before or if i would use some type of command where the input to control my character is released, causing it to go to a certain frame with the command “set_sprite_frames(value)”. I’m not too sure how to work this out since i’m new to the godot engine, so if anyone could give me some pointers it would be greatly appreciated.

:bust_in_silhouette: Reply From: SnapCracklins

I would do a state machine and then cycle through those. I am working on a 2d character that has run, walk and idle animations.

var playerState = STATE.IDLE

func _process(delta):
     match playerState:
                # idle command/animation here
                # walking
                # running

You can add however many states you want then use whatever logic you prefer to change the Player state as necessary. You can also run the match logic in different functions to keep your animations/commands decoupled (which I’d recommend for your own sanity).