Matthew Pontarolo
Matthew Pontarolo

Reputation: 13

Detecting Objects in a radius

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?

Upvotes: 1

Views: 358

Answers (2)

James McGrath
James McGrath

Reputation: 317

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;

Upvotes: 0

Related Questions