Tapping outside of Button triggers Button anyway - how to prevent?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hello community,

I created a „custom-button“ (decorated with some nice animations) for mobile devices (so for touch screen).

|- CollisionShape2D
|- Sprite

Here’s the code:

extends Node2D

func _on_Area2D_input_event(_viewport, event, _shape_idx):
	if event is InputEventScreenTouch:
		if event.is_pressed():
			print ("Tapped!")
			print ("Released!")

func _on_Area2D_mouse_exited():
	if Input.is_mouse_button_pressed(1):
		print ("Canceled!")

It works perfectly exept for one strange issue: when I tap anywhere on the screen (= outside the collision shape) after releasing the button, it triggers the “mouse_exited“-action. And only once – further taps do nothing (until I release the button again…).
I can’t figure out why that happens. How could I prevent this? I would like any input to be registered on the collision shape only.

I’d very much appreciate any suggestions!

I cannot reproduce your problem. Can you upload an example project with the issue?

njamster | 2020-08-08 22:16

Sure! If only I knew how… (as you can tell, I’m very new to Godot and all this). Could you tell me how (and where) to upload?

pferft | 2020-08-09 10:05

You can upload the whole project directory as is. If you don’t know where it’s located, you can open the project in Godot and right-click on “res://” in the FileSystem-tab and then choose “Open in File Manager”. As for where: any site where you can upload files will do, e.g. Easyupload.io, WeTransfer or GoFile - just google “free file upload”. These pages will provide you with a link that you can share here for others to take a look. If you know how, it’s also usually a good idea to create a zip-file of the project directory instead of uploading all of the files individually - some services may even require zips.

njamster | 2020-08-09 20:06

I wasn’t sure if I could upload directly somewhere on this site, thanks for clarifying. And thank you for your effort! Here’s the link: WeTransfer - Send Large Files & Share Photos Online - Up to 2GB Free

I have “Emulate Touch From Mouse” disabled here and I test on a hp envy convertible touch screen as well as on a Samsung Galaxy A3 (GLES 2 - working towards mobile-apps and generally aiming for functionality on older phones as well).

Thanks for your help!

pferft | 2020-08-10 07:58

Here is another test example with some comments:
WeTransfer - Send Large Files & Share Photos Online - Up to 2GB Free

Is there no way to change ‘if event.is_pressed():’ to maybe something like ‘if event.is_tapped():’…? In case ‘tapped’ would only trigger one frame? Wouldn’t that solve the problem?

Of course all this wouldn’t be necessary if only the whole screen would be ignored anyway and just the Area2D would be clickable!

I just realized that it works perfectly on my phone. The issue remains on the PC-touchscreen though. Any ideas why?

pferft | 2020-08-12 13:07