The death animation of my character is repeatedly being activated, never allowing the animation to finish, and thus causing an unending loop. I have figured out that this is because the physics function I am checking is being checked hundreds of times a second, never allowing the animation to finish before it plays it again.
I have tried creating a separate function to be called under specific conditions, and have tried altering the conditions multiple times to see if the looping can be avoided. Below are my two main functions, the physics function where most of the actions are happening and the death function, where (as you can imagine) the death related features are implemented. I could have nested the code in the death function inside the physics function but I thought that separating them might solve the problem. It did not. I attempted to check if the death animation is already playing but this seemed to have no impact: if get_node("AnimatedSprite2D").animation != "Hurt":
My next idea is that the difference between the "AnimatedSprite2D" and the "AnimationPlayer" is causing some issues, causing them to either both change the animation or something else along those lines. I am not to sure how to proceed and any help/explanation would be beneficial in helping me understand this problem more. If you also have any beginner friendly advice involving the rest of the code, I would accept any constructive criticism. Thank you!
func _physics_process(delta):
if alive == true:
if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
if get_node("AnimatedSprite2D").animation != "Hurt":
get_node("AnimationPlayer").play("Jump")
var direction = Input.get_axis("ui_left", "ui_right")
if direction == -1:
get_node("AnimatedSprite2D").flip_h = true
elif direction == 1:
get_node("AnimatedSprite2D").flip_h = false
if direction:
velocity.x = direction * SPEED
if velocity.y == 0:
if get_node("AnimatedSprite2D").animation != "Hurt":
get_node("AnimationPlayer").play("Run")
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if velocity.y == 0:
if get_node("AnimatedSprite2D").animation != "Hurt":
get_node("AnimationPlayer").play("Idle")
if velocity.y > 0:
if get_node("AnimatedSprite2D").animation != "Hurt":
get_node("AnimationPlayer").play("Fall")
move_and_slide()
if health <= 0:
death()
func death():
alive = false
if get_node("AnimatedSprite2D").animation != "Hurt":
get_node("AnimatedSprite2D").play("Hurt")
await get_node("AnimatedSprite2D").animation_finished
self.queue_free()
get_tree().change_scene_to_file("res://main.tscn")