Need Help With Making an Animation Play When I Press a Button

Godot Version

4.7.1 (Mobile)

Question

I am currently making a small mobile game and need to add buttons to play a specific animation. I am very new to this, but i already knew i had to attach a script and a signal to the button.

I just need the steps to complete this. Any feed back is appreciated!

extends Node2D


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass


func _on_button_pressed() -> void:
	pass # Replace with function body.

In your other thread this tutorial was mentioned. It includes how to create an AnimatedSprite2D with animations, and how to play them through code.

You say you want to play an animation, but we don’t know anything about your animation, so it’s rather difficult to give advice. Generally the animation will live on a child node, you’ll grab that with the $ symbol and use it’s play method, with the specific animation name you want. It all makes a lot more senes if you check out that tutorial

func _on_button_pressed() -> void:
    $MyAnimationNode.play("A_Specific_Animation")

Try not to make duplicate threads

Thank you!!! I am sorry i am terrible at giving details, but the code works!

Have a blessed day!