Animation overlapse

Godot Version

4.4

Question

How can I fix it? I tried a few ways but it didn’t work. The else part is broken.


@onready var anim: AnimatedSprite2D = $AnimatedSprite2D

@export var speed:int = 5
var direct:String = "idle_down"
var phone:bool = false

func _ready() -> void:
    anim.play(direct)
    

@warning_ignore("unused_parameter")
func _process(delta: float) -> void:
    movement()
    actions()


@warning_ignore("unused_parameter")
func actions():
    if Input.is_action_just_pressed("phone"):
        if phone == false:
            anim.play("phone_out")
            phone = true
        elif phone == true:
            anim.play("phone_in")
            await get_tree().create_timer(1).timeout
            phone = false
            anim.play(direct)

func movement():
    var input_vector:Vector2 = Vector2.ZERO
    var is_moving:bool = false
    if Input.is_action_pressed("up"):
        direct = "idle_up"
        anim.play("walk_up")
        input_vector.y -= 1
        is_moving = true
    elif Input.is_action_pressed("down"):
        direct = "idle_down"
        anim.play("walk_down")
        input_vector.y += 1
        is_moving = true
    elif  Input.is_action_pressed("right"):
        direct = "idle_right"
        anim.play("walk_right")
        input_vector.x +=  1
        is_moving = true
    elif Input.is_action_pressed("left"):
        direct = "idle_left"
        anim.play("walk_left")
        input_vector.x -= 1
        is_moving = true
    else:
        anim.play(direct)
        is_moving = false
        
    
    input_vector = input_vector.normalized()
    
    velocity = input_vector*speed
    move_and_slide()

you should have a look at state_machines. They are very useful for these kind of scenarios. There are a lot of possible implementation tutorials on youtube

1 Like