how to return to idle animations after animations run

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

important: I’am using AnimatedSprite
I made animation via documentation godot link: 2D sprite animation — Godot Engine (stable) documentation in English
but in with documentation do only animation run if you input key right and stop animation but i wanna if you not input any key: animations idle
my code:

extends KinematicBody2D

onready var _animated_sprite = $AnimatedSprite

func _process(_delta):
	if Input.is_action_pressed("ui_down"):
		_animated_sprite.play("run")
	elif Input.is_action_pressed("ui_up"):
		_animated_sprite.play("run")
	elif Input.is_action_pressed("ui_left"):
		_animated_sprite.play("run")
	elif Input.is_action_pressed("ui_right"):
		_animated_sprite.play("run")
	else:
		_animated_sprite.stop() 

(I’m add keys up, down and left)
thanks for help <3!

Edited to fix code formatting…

jgodfrey | 2022-12-28 22:43

:bust_in_silhouette: Reply From: jgodfrey

That else block will execute if none of the 4 input keys are being pressed, so you could replace this:

_animated_sprite.stop()

With something like:

if !_animated_sprite.playing(): 
    _animated_sprite.play("idle")

what if i am using animation player, every animation work properly but not the attack, it only show millisecond of the animation and then return to idle animation
here is the code:

extends KinematicBody2D

var velocity : Vector2

export var max_speed : int = 600
export var gravity : float = 35
export var jump_force : int = 1600
export var acceleration : int = 50
export var jump_buffer_time : int  = 15
export var cayote_time : int = 15

var jump_counter : int = 0
var jump_buffer_counter : int = 0
var cayote_counter : int = 0

func _physics_process(_delta):

if is_on_floor():
	cayote_counter = cayote_time
	jump_counter = 0

if not is_on_floor():
	if cayote_counter > 0:
		cayote_counter -= 1
	
if jump_buffer_counter > 0 and jump_counter < 1:
	jump_counter += 1
	cayote_counter = 1

velocity.y += gravity
if velocity.y > 2000:
	velocity.y = 2000

if Input.is_action_pressed("right"):
	velocity.x += acceleration 
	$Sprite.flip_h = false
	get_node("AnimationPlayer").play("run")
elif Input.is_action_pressed("left"):
	velocity.x -= acceleration
	$Sprite.flip_h = true
	get_node("AnimationPlayer").play("run")
elif Input.is_action_pressed("jump"):
	get_node("AnimationPlayer").play("jump")
elif Input.is_action_just_pressed("attack"):
	attack()
else:
	velocity.x = lerp(velocity.x,0,0.2)
	get_node("AnimationPlayer").play("idle")

velocity.x = clamp(velocity.x, -max_speed, max_speed)

if Input.is_action_just_pressed("jump"):
	jump_buffer_counter = jump_buffer_time
	get_node("AnimationPlayer").play("jump")

if jump_buffer_counter > 0:
	jump_buffer_counter -= 1

if jump_buffer_counter > 0 and cayote_counter > 0:
	velocity.y = -jump_force
	jump_buffer_counter = 0
	cayote_counter = 0

if Input.is_action_just_released("jump"):
	if velocity.y < 0:
		velocity.y += 400

velocity = move_and_slide(velocity, Vector2.UP)


func attack_detected():
print("hit")

func attack():
get_node("AnimationPlayer").play("Attack")
print("attack")


func _on_AttackBox_body_entered(body):
if body.has_method('attacked'):
	body.attacked()

Mhd08 | 2023-06-17 15:24