Godot Version
4.3
Question
On button press, my attack animation keeps looping despite disabling the loop in the animation player and the only way to stop the loop is to press the same button again, however, I would like the animation to not loop at all.
Parent script:
extends Node2D
@export var player: Node2D
@export var enemy: Node2D
@onready var question: Label = $CanvasLayer/Control/VBoxContainer/Question
@onready var answer_1: Button = $CanvasLayer/Control/HBoxContainer/Answer1
@onready var answer_2: Button = $CanvasLayer/Control/HBoxContainer/Answer2
@onready var answer_3: Button = $CanvasLayer/Control/HBoxContainer/Answer3
var lives = 3
var correct_answer: int
func _ready():
# Prepare characters
player.connect("enemy_hit", enemy.take_damage)
enemy.connect("player_hit", player.take_damage)
# Show question
start_new_question()
func start_new_question():
var num1 = randi() % 10 + 1
var num2 = randi() % 10 + 1
correct_answer = num1 + num2
var wrong_answer1 = correct_answer + (randi() % 5 - 2)
var wrong_answer2 = correct_answer + (randi() % 5 - 2)
var answers = [correct_answer, wrong_answer1, wrong_answer2]
answers.shuffle()
question.text = str(num1) + " + " + str(num2) + " = ?"
answer_1.text = str(answers[0])
answer_2.text = str(answers[1])
answer_3.text = str(answers[2])
func on_button_pressed(answer):
if answer == correct_answer:
player.attack()
else:
enemy.attack()
#func end_game():
#$GameOverScreen.show()
func _on_answer_1_pressed() -> void:
on_button_pressed(int(answer_1.text))
func _on_answer_2_pressed() -> void:
on_button_pressed(int(answer_2.text))
func _on_answer_3_pressed() -> void:
on_button_pressed(int(answer_3.text))
Child script:
extends Node2D
signal enemy_hit
@onready var animation_player: AnimationPlayer = $AnimationPlayer
var health: int = 3
func _process(delta: float) -> void:
if !animation_player.is_playing():
animation_player.play("idle")
func attack() -> void:
animation_player.play("attack")
func take_damage() -> void:
health -= 1
animation_player.play("hit")
if health <= 0:
die()
func die() -> void:
pass
func notify_enemy_hit():
emit_signal("enemy_hit")
I have tried using flags but to no avail.