Help with Animation looping

Godot Version

Replace this line with your Godot version

Question

I’m very new to Godot, and I’m trying to follow the 2D Your First Game tutorial with my own artwork instead of the artwork given by the tutorial. I’m trying to animate my AnimatedSprite2D to not loop, and instead just start a movement when the left key is pressed, and stay in that end position until the key is no longer being pressed.

I tried just stopping the animation looping in the GUI but I don’t know if I have to change my script as well?

This should work, what happened when you tried to run the game? Can you share your script?

This is the script! It doesn’t really have any discernible difference. I only have 3 animation frames per animation, do I need more?

extends Area2D

@export var speed = 400
var screen_size

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	screen_size = get_viewport_rect().size


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	var velocity = Vector2.ZERO
	if Input.is_action_pressed("move_right"):
		velocity.x += 1
	if Input.is_action_pressed("move_left"):
		velocity.x -= 1
	if Input.is_action_pressed("move_down"):
		velocity.y += 1
	if Input.is_action_pressed("move_up"):
		velocity.y -= 1
		
	if velocity.length() > 0:
		velocity = velocity.normalized() * speed
		$AnimatedSprite2D.play()
	else:
		$AnimatedSprite2D.stop()
		
		
	if velocity.x < 0:
		$AnimatedSprite2D.animation = "left"
	elif velocity.x > 0:
		$AnimatedSprite2D.animation = "right" 
	elif velocity.y < 0:
		$AnimatedSprite2D.animation = "up"
	elif velocity.y > 0:
		$AnimatedSprite2D.animation = "up"


	position += velocity * delta
	position = position.clamp(Vector2.ZERO, screen_size)

Ah I see, _process happens every frame, so it’s being told to play the animation every frame. Godot won’t restart an animation if it’s already playing, but it will play again if the animation finished.

You may have to use different code as part of _input to only detect when a button is first pressed down.

func _input(event: InputEvent) -> void:
	# echo events are when a key is held down and repeats typing
	if event.is_echo():
		return # ignore echos

	if event.is_action_pressed("move_left"):
		$AnimatedSprite2D.play("left") 
	elif event.is_action_pressed("move_right"):
		$AnimatedSprite2D.play("right")