0 votes

I want an action to be executed when a sprite is clicked (with touch). I have tried to solve this with the following post (https://godotengine.org/qa/3825/check-if-sprite-self-is-clicked) but unfortunately it does not work as expected... It does not react to anything anymore. With my previous code I was able to capture at least a touch input on the whole screen.
Here is my code where the touch input on the hole screen works and how I built the scene.

extends Area2D

func _ready():
    pass # Replace with function body.

func _input(event: InputEvent) -> void:
    if event is InputEventScreenTouch and event.is_pressed():
        print("clicked")

Scene Build pictures: https://imgur.com/a/t6nB2vn

This scene

I want to instantiate the scene with the sprite into the main scene, where by pressing the sprite values should be changed.

Code of the main scene:

extends Node

onready var touches_label : Label = $Container/VBoxContainer/Touches

var click_count := 0.0

func _ready() -> void:
    update_touches_label()

func update_touches_label() -> void:
    touches_label.text = str(click_count)
in Engine by (23 points)
edited by

3 Answers

+1 vote
Best answer

Oh okay then try this. Area2D has a node method called input_event(). Connect it.

func _input_event(viewport, event, shape_idx):
by (109 points)
selected by

Now it finally worked!
I started a new project and just inserted the sprite and tested it. I guess that the container or something else in between sparked and therefore it didn't work.
Thanks a lot for your help!

+3 votes

Hey, I also had a problem that when I used touch, nothing ever happened.
I was able to solve it and it currently looks like this:

func _input(event):
   if event is InputEventScreenTouch and event.is_pressed():
by (109 points)

Oh, I'm sorry I didn't look at the translation again.
So the touch recognition sort of works... But only on the full screen. I want the sprite to only detect the touches.

+1 vote

After a lot of research I finally found out why the sprite did not recognize the touch when a control node was added. To make the sprite recognize the touch you have to set the filter of the control node in the inspector under mouse to "Ignore".

Overwiev of the Config

by (23 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.