How to drag and drop one by one using area2d?

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

Hello guys, i have a problem on my drag and drop project using area2d, when 2 object is intersected, its hard to drag one by one, i think the problem is when area2d reading click using collision shape, but when we click on the intersected area of 2 collision shape, there are 2 area2d reading it, how can i solve the problem?

:bust_in_silhouette: Reply From: njamster

Take a look at this answer. You’d attach a script to your Area2D-nodes:

extends Area2D

var mouse_over = false

func _ready():
	connect("mouse_entered", self, "_mouse_over", [true])
	connect("mouse_exited", self, "_mouse_over", [false])

	set_process_unhandled_input(true)

func _unhandled_input(event):
	if mouse_over and event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
		get_tree().set_input_as_handled()
		print("clicked")

func _mouse_over(value):
	self.mouse_over = value

In this example the Area2D will react to any click of the left mouse button, as long as it happens inside the Area2D’s CollisionShape2D (that’s ensured by the mouse_over-value). It will set the input as handled, so a second, overlapping Area2D will not get the event as well. The Area2D higher up in the tree will get the event first.

For a more concrete answer, you’ll have to provide more details.

thanks for this solution i’ll try it

wahyuadiramadan | 2020-03-04 13:39