How to handle multiple overlapping Area2D nodes using the mouse_entered signal?

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

I’m creating a card game and there are several cards that overlap for aesthetic reasons and I want to highlight one card at a time. Each card fires a signal when the cursor enters the Area2D node and this often means that there’s usually two cards that fire the mouse_entered signal at the same time. I’ve tried assigning each card a different z-index and using the get_overlapping_areas() function from Area2D but this has its own problems. Using the overlapping areas function returns all overlapping cards which includes other cards that the mouse area hasn’t entered.

See: Example. The green represents the cursor position of the overlapping cards and the red represents all of the Area2D nodes returned from the get_overlapping_areas() function. I tried getting the mouse position to limit the overlapping areas to a point that the cursor intersects at but there doesn’t seem to be anything in Area2D that allows for checking intersecting points. The other problem is that when each mouse_entered signal activates, I’m basically looping through all of the overlapping areas twice which doesn’t seem efficient to me. Is there a way to make this work or a different approach that would work for something like this?

My current code:

func _on_Area2D_mouse_entered(card, mouse_pos):
    var cards = card.area.get_overlapping_areas()
    var top_z = 0
    var top_card = card.area
    for card_z in cards:
	    if card_z.z_index > top_z:
		    top_card = card_z
		    top_z = card_z.z_index


This causes the right most card to be highlighted and it’s not a card that the cursor is hovering over.

Any luck to solve that? I need to solve the same problem. Thank you.

DavidPeterWorks | 2023-04-18 13:17