can i play sound while touch on object intersection ?

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

I create a project for android devices.
object A is in front of object B. Both play SFX when I try to touch the part between the two objects (see picture, mark red circle), I want to play SFX on object A when it touches the part between the two objects. please help me
mark red

I do not see the Image. Maybe add some code?

clemens.tolboom | 2021-04-23 16:08

I have two Area2D with input event touch screen, both object play SFX. When I touch on the area (mark red on the picture) both objects play SFX, I want just object on front only play SFX, Object A in front of object B.
mark red

irwant | 2021-04-24 02:50

:bust_in_silhouette: Reply From: Poobslag

There are several possible ways to implement something like this

  • Have some code on Object A which stops the sound which Object B played
  • Have some code on Object B which checks if you’re touching Object A, before playing a sound
  • Have some kind of “Sound Manager” node which knows about Object A and Object B, and has conditional logic to play one of the two sounds
:bust_in_silhouette: Reply From: clemens.tolboom

Reading the class-node-method-input docs you can stop the event to get handled by other nodes

To consume the input event and stop it propagating further to other nodes, SceneTree.set_input_as_handled() can be called.

So you can add this code SceneTree.set_input_as_handled() to your touch event handler for both A and B.

this method work when I change the node type from Area2D to Node2D and change the sprite to touchbutton then delete collision2D

irwant | 2021-04-26 05:00