How to touch a specific node2D (sprite2D or something that can be animated)?

Godot Version

4.2.2

Question

Hello! I’m new to Godot, so I want to know if anyone know the better way I can approach this mechanic: https://preview.redd.it/how-to-touch-a-specific-node2d-sprite2d-or-something-that-v0-r9w3bzl1x1ed1.png?width=1200&format=png&auto=webp&s=b9e6f01b061d2546617e352972bdad372f696a64

The idea is to have a node that can be animated (preferably with spritesheet), press and hold until animation loading finish, fade out that node and make other smaller copies of that “button” spread randomly across the screen. That copies also can be touchable, but not hold. Just a tap and disappears.

I know how to verify that the user is touching the screen and add the signal so that it changes state after a certain amount of time, but I don’t know how to reference the specific node that I want to be touched.

I tried using rect2 methods, but they didn’t work. Not really sure if the problem is the button structure or if the type of node is incorrect… If you can give me any recommendations or tutorials, it would be helpful.

I’ve seen on forums that some people use Area2D, but I’m not too convinced by the idea. The most important thing is to find a way that is mobile-friendly and capable of playing an animation. And, if you need more info, let me know.

Many thanks.

Have you considered combining a AnimatedSprite2D with either a area2d or a control-node?

1 Like

The problem is about the script, I don’t know how to reference the node I need. I was using an AnimatedSprite2D inside a Character node, but the script I saw in tutorials doesn’t work (get_rect) with that node

What exactly do you want to reference? The animatedSprite?

@onready var animatedSprite = $AnimatedSprite2D # The name of the node

I know that the explanation is very general, so I will try to present it as a concept:

func _input(event):
    if event is InputEventTouchScreen and event.pressed:
        #if touchableNode is pressed then:
            #do something...

The commented lines are the script I need to figure out. I try to use the method get_rect(), but apparently it’s not able to use it on AnimatedSprite2D.

Other people use an Area2D and check if the user taps inside the “button” area. But, I wonder if that would be inefficient in some way?

not really. But might as well use a control node. It has a gui_input signal that emits when its recieves inputs:

func _on_gui_input(event):
    if event is InputEventTouchScreen and event.pressed:
         # your code here
1 Like

I tried but doesn’t work :confused: I made an AnimatedSpride2D inside a Control node, put a signal inside control script (_on_gui_input(event)). Also tried choosing he animated sprite to receive the signal with _on_control_gui_input(event) and nothing happen.

You have to make the control node the size of the sprite

1 Like

Never mind, it works. I moved the sprite instead of the control node by mistake. Thank you!

1 Like

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