Topic was automatically imported from the old Question2Answer platform.
Asked By
pferft
Hi everyone,
the issue is: tapping on a touch-screen is being registered anywhere, but not only on the object I’d like to.
So if I had an Area2D (with a CollisionShape attached) in my scene, would there be a simple way of having taps registered only in this area (and not on the “rest of the screen”)?
Add a touch screen input as a child. Add a shape to it and and make it the size of the Area2D.
I had the same idea earlier, but couldn’t quite figure out how to make it work. The scene is set up like this:
Node2D # the script is attached here
|
|-- Area2D
|
|-- CollisionShape2D
|-- (and a sprite here)
And my simple script is:
extends Node2D
func _input(event):
if event is InputEventMouseButton:
if event.is_action_pressed("mousebuttonclick"):
print("clicked!")
if event.is_action_released("mousebuttonclick"):
print("released!")
Where would you suggest inserting the child (and which one would be best suited for a touch screen input?)
Thanks for your help!
pferft | 2020-08-16 12:12
You can make it a child of Area2D. You can add a shape or an image to get the size to match the size of the Area2D.
Add signals for pressed and release, you don’t need the _input event.