It is possible to compare the Click position of my mouse with a position of an element in the array?

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

Hello i’m having a little trouble on a code i’m working on.
I would like to know if it is possible to compare the position of an element that is in the Array with the position of the mouse click.
And also I would like to know if it is possible to make an Array using elements of the scenery such as nodes or sprites.

:bust_in_silhouette: Reply From: IHate

You can but, what do you mean by compare? You want to measure the distance between mouse and object?

If you want to check the distance between the mouse and object3 in array when you click you could do like this:

array = [object1,object2,object3]

func _input(event):
 if event.is_action_pressed("mb_left")

Keep in mind you have to set the clicks on your godot project at:
project > project settings > Input map and add the mouse buttons with the name you want in my example "mb_left" also keep in mid get_global_mouse_position() is not always available depending on what node your inheriting from you get this method from CanvasItem and nodes that inherit from CanvasItem.

And for your second question. Yes you can, sprites are nodes too. All you need to do is get the node you want with get_node("insert path or name of the node") and then you can append it to an array.

I understand you were a great help thank you very much and yes I wanted to measure the distance between the click of the mouse with the position of the sprite on the screen

GBboss | 2020-09-13 18:02

What I have to do is every time I click on an image the program identifies the image by scanning the array after that the color of the image changes to a random color.
And one more thing i’m brazilian so sorry if my ingels is kinda wrong.

GBboss | 2020-09-13 18:21

I’m spanish and my english is not the best either xd

IHate | 2020-09-14 00:05