How to stop animated sprite when player stops moving

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

hi so bassicaly I need help with animated sprite
So im doing some 2d top down game and im trying to make animated sprite to loop when player is moving.

if Input.is_action_pressed("ui_left"):
	motion.x = -1 * movespeed
	$AnimatedSprite.play("walk")

elif Input.is_action_pressed("ui_right"):
	motion.x = 1 * movespeed
	$AnimatedSprite.play("walk")

elif Input.is_action_pressed("ui_up"):
	motion.x = -1 * movespeed
	$AnimatedSprite.play("walk")

elif Input.is_action_pressed("ui_down"):
	motion.x = 1 * movespeed
	$AnimatedSprite.play("walk")

here is my code
+i would need code stop the animated sprite on the last frame and wait for the player to move again to resume the animated sprite

:bust_in_silhouette: Reply From: alexp

You can let the animation stop at the last frame with
$AnimatedSprite.frames.set_animation_loop("walk", false)

You can probably put that in the _ready() function since you’ll be calling play() as soon as it ends if you’re holding the button, so you shouldn’t have to worry about telling it to loop. If it doesn’t reset as intended, you can connect a function to the animation_finished signal, and call set_frame(0) and stop()

:bust_in_silhouette: Reply From: iikl07_

I don’t know if you mean this code:

extends KinematicBody2D

var speed = 50
var motion = Vector2.ZERO

func _physics_process(delta):
	if Input.is_action_pressed("ui_right"):
		$AnimatedSprite.play("sidwalk")
		$AnimatedSprite.flip_h = true
		motion.x = speed
		motion.y = 0
	elif Input.is_action_pressed("ui_left"):
		$AnimatedSprite.play("sidwalk")
		$AnimatedSprite.flip_h = false
		motion.x = -speed
		motion.y = 0
	elif Input.is_action_pressed("ui_down"):
		$AnimatedSprite.play("downwalk")
		motion.y = speed
		motion.x = 0
	elif Input.is_action_pressed("ui_up"):
		$AnimatedSprite.play("upwalk")
		motion.y = -speed
		motion.x = 0
	else:
		$AnimatedSprite.set_frame(0)
		$AnimatedSprite.stop()
		motion.x = 0
		motion.y = 0
	
	move_and_slide(motion)

Note: I made the code myself and I’m not sure it works well

This code stops the player’s movement in Farme 0. You can control the appropriate window to stop the player’s movement. This helps improve the gaming experience by keeping the player’s face in the same place where they were clicked

enter image description here

iikl07_ | 2023-04-27 21:21