I’ve tried every way to make the character attack and it doesn’t just happen in the first frame of the animation. So I got this result where all the animation occurs, but the code crashes. Can you help me correct this or recommend another line of reasoning?
If I understand you correctly, your attack animation only plays the first frame.
I haven’t done any sprite animation in Godot yet. However, from looking at the documentation it looks like you need to call play()
on every frame that you want the animation to run. Because _input()
only runs when an input is registered (e.g. when you press your attack
-button), your animation will only play when you press something.
To fix your problem (simply), you could make a timed animation like so:
@onready var attack_timer = $YourTimerName
# I took the liberty of renaming the "ataque" function.
func attack(event):
if event.is_action_just_pressed("attack"):
attack_timer.start()
# You could insert some attack-code here as well.
# (No point in swinging if it doesn't do any damage, am I right >.>)
func _input(event):
# In your current code, you are checking the "attack"-input
# twice which is not ideal (once in _input() and once in ataque()).
# This has been resolved here.
attack(event)
func _process(delta):
# The timer controls how long the animation will play for.
# This is a very simple system that makes use of Godot's Timer node
if attack_timer.time_left > 0:
sprite.play("attack")
# If you want, you could even use the timer as a means to control
# the character's attack frequency.