0 votes

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 mouseentered signal at the same time. I've tried assigning each card a different z-index and using the getoverlapping_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 getoverlappingareas() 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.

in Engine by (12 points)

Please log in or register to answer this question.

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.