[SOLVED] Sprites Distance Array

I have a big request for you.
I have the player (in black) and two groups of objects: the red group and the green group.
The goal of the code should be: by releasing the spacebar the system calculates of the objects in the red group the closest one (the fish).
image

Pressing ALT the selected object should be the one that is the least distant (the cocktail), pressing ALT the one that is furthest away, the cup, and pressing ALT it returns to the fish.
image

image

image

I would like to write dynamic code that can handle multiple objects (not only 3, but all the group object).
In theory, the code should be structured like this:
1 Releasing spacebar the system inserts the red objects into an array of sprites.
2 It sorts the array by distance from the player
3 It defines a variable as read index of the array to 0 ( ex: VariableObjectIndex )
4 It reads the array at the position indicated by VariableObjectIndex and move the box to the position of the object.
5 By pressing ALT, It adds 1 to VariableObjectIndex.
6 If VariableObjectIndex > number of objects in the array VariableObjectIndex = 0
7 goto line 5

My problem is how to populate an array with sprites or to find a workaround that allows me to handle this requirement.

Thanks,
J

  1. Add a number object variable to all the red objects called “Id”, and default it to -1.
  2. Put the red objects in an object group, imaginatively called “RedObjects”.
  3. Then:
1 Like

Wow, you are a genius!
I knew all the commands but I couldn’t manage them to achieve the goal.
In Italy we say ‘I have to buy you a coffee’.

Thank you very much,
J