1

I need to have a point check for objects around it based on distance, and it is not possible to determine what will be on the stage at any given time, so I cannot just trace everything that would near it.

How would I do this so it can also detect what the nearby object is, in addition to detecting the object?

  • Duplicate question: http://stackoverflow.com/questions/481144/equation-for-testing-if-a-point-is-inside-a-circle – awiebe Aug 20 '12 at 03:36

2 Answers2

1

Use Pythagorean, like in this example:

http://www.flepstudio.org/forum/tutorials/501-pythagorean-theorem-actionscript-3-0-a.html

Shefy Gur-ary
  • 628
  • 8
  • 19
0

I'm not sure how many objects you'll be having on the screen at any one time, but how about cycling through all the children in the movieclip/stage and checking against each one. Something like-

function prox(limit:int):MovieClip{
    for(var i:int = 0; i<stage.numChildren;i++)
        if(Math.abs(MovieClip(stage.getChildAt(i)).x - point.x) < limit && 
           Math.abs(MovieClip(stage.getChildAt(i)).y - point.y) < limit){
            return MovieClip(stage.getChildAt(i));
        }
    }
}

Or you could extend that to returning an array of MovieClips by just changing the return type

function prox(limit:int):Array{

adding an array var and changing the code inside the if to

array.push(MovieClip(stage.getChildAt(i));

and

return array;
James McGrath
  • 187
  • 1
  • 4
  • 15
  • That could be problematic, as there could be anywhere from 5-50 movie clips to be checked. Would that amount be an issue? – Matthew Pontarolo Aug 23 '12 at 01:15
  • You might find that a larger amount of movieclips may slow it down, but I haven't tested the efficiency of this code. An alternative could be to have an invisible extension of the area around that point, and call a hit test function between that object and the array of objects. – James McGrath Aug 23 '12 at 07:16