Animations Overriding Each Other?

Godot Version

4.3

Question

I am trying to add a attack animation for my game, but instead of attacking the idle and/or the running animation just interrupts it, all you can see when you click is the pixels moving for the first frame of the attack. (note I am only 1 week into Godot so pls don’t make fun if my coding is bad)
Code:
extends CharacterBody2D

@export var speed = 300
@export var gravity = 20
@export var jump_force = 300

@onready var ap = $AnimationPlayer
@onready var sprite = $Sprite2D

var attack = false

func _physics_process(_delta):
if !is_on_floor():
velocity.y += gravity
if velocity.y > 1000:
velocity.y = 1000

if Input.is_action_just_pressed("Attack_Input"):
	if is_on_floor():
		attack = true
		ap.current_animation = "Attack_Animation"
		await("Idle_Animation")


if Input.is_action_just_pressed("Jump_Input"): #&& is_on_floor():
	if is_on_floor():
		velocity.y = -jump_force

var horizontal_direction = Input.get_axis("Move_Left_Input", "Move_Right_Input")
velocity.x = speed * horizontal_direction


if horizontal_direction != 0:
	switch_directions(horizontal_direction)

sprite.flip_h = (horizontal_direction==-1)

move_and_slide()

update_animations(horizontal_direction)

if Input.is_action_just_released("Run_Input"):
	if is_on_floor():
		speed = 125


if Input.is_action_just_pressed("Run_Input"):
	if is_on_floor():
		speed  = 175

func update_animations(horizontal_direction):
if is_on_floor():
if horizontal_direction == 0:
ap.play(“Idle_Animation”)
else:
ap.play(“Run_Animation”)
else:
if velocity.y < 0:
ap.play(“Jump_Animation”)
elif velocity.y > 0:
ap.play(“Fall_Animation”)

func switch_directions(horizontal_direction):
sprite.flip_h = (horizontal_direction == -1)
sprite.position.x = horizontal_direction * 4

There is not much too the game yet because the past 2 days I have just been stuck with this error, any help will be appreciated.

Please mark your code with ``` at the beginning and end so that it can be read. You can use image
But for now I think it helps if you replace is_action_just_pressed with is_action_pressed

at first , i had a same matter. this is very easy.

you just need to this :
if attack == false:
|>$Sprite2D.play(“idle”)
and any more codes about walking and jumping

i am sorry for my bad english skill

good luck

and you also should to use AnimatedSprite signals and this function:
func on_AnimatedSprite_finnished():
|>attack=false

dont forgot to choose its signal

I tried to do this and it didn’t work, it either never stops the attack animation or does the same problem of looping over and over again

I got it to work, forgot to add the signal to the AnimatedSprite tysm

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.