21 Replies Latest reply on Nov 4, 2007 9:00 PM by kglad

# Need to find nearest movie clips

Hi all,

I have an app which has between 30 and 200 nearly identical movie clips on the stage at any time.

I need to get an array of the top 10 movie clips which have the closest global position (X/Y) to any given clip.

Obviously I could do this with a lot of sorting. And I could do it with a server call, and use PHP, since it has advanced multidimensional array sorting methods. But I'd like to avoid server calls, as they will slow the app down.

Is there some high-level way to do this in Actionscript? I'm using Flash MX (6).

Perhaps collision detection methods can be used?

-joshua
staff@dot-comet.com
• ###### 1. Re: Need to find nearest movie clips
dot,
First you have to make sure you can find the distance of any two movie clips from each other. Some simple geometry will do that for you, with the pythagorean theorem will do that for you. If you maintain an array of all the movie clips, but adding to it each time one is added to the stage, you can write a function that will run through the array and check which are the top 10 closest. Unfortunately, I am not sure of the scripting in MX, so I can't write an example. But hopefully this will give you an idea.
• ###### 2. Re: Need to find nearest movie clips
there's no easy way to do this with actionscript.

even finding the distance between two movieclips (which generally means finding the minimum distance between all pairs of points with one of the pair in one movieclip and the 2nd of the pair in the other movieclip), while easy conceptually and easy to code, is computationally intensive.

so, finding a practical method to do this for all pairs of movieclips is even more computationally intensive. so much so, that you must code this cleverly or you'll slow the host computer to a crawl (in addition to generating some messages about script taking too long).
• ###### 3. Re: Need to find nearest movie clips
Here's an example method I'd come up with to achieve this, using the Point.distance method combined with an Array.sortOn method, wherein the first ten elements of the array become the closest in value by distance.

Check it out :) FLA
• ###### 4. Need to find nearest movie clips
EDIT: aww crap! sorry for the double post.
• ###### 5. Need to find nearest movie clips
oops, slight change to code, in the FLA, in the output for loop it should be, i<10, it's was only returning the top 9, LOL!!!

PS. still fairly fast even with a 1000 instances on Stage.
• ###### 6. Re: Need to find nearest movie clips
it can't be fast unless you make some significant assumptions. like the distance between two movieclip's registration points is related to the distance between the two movieclips (which usually is not true).
• ###### 7. Need to find nearest movie clips
I'm clocking it (simple) at 1150 ms, for a 1000 instance check :)

PS. that also is including a colorTransform for the top ten.
• ###### 8. Re: Need to find nearest movie clips
Hey, thanks guys!!

Actually, there won't be more than 200 instances, and usually less. So, this might work.

Thanks again!! I'll let you know how it goes. Kindest regards,
-joshua
• ###### 9. Need to find nearest movie clips
I'm running a 1Ghz Mac ibook, Flash8.

Here's a new example, wherein there are five comparisions taking place on 1000 instances, in usually around 2.5s, and about .75s are usually taken to propagate the 1000 instances. So this is making 5000 comparisions in under 2s.

Here's the file: FLA

PS. granted, that's not lightning fast or anything, but come on kg, ya got give somethin' for that :) LOL!!!
PSS. it'll do a 200 instance comparision around 250ms ... on five MCs ... hee hee!
• ###### 10. Re: Need to find nearest movie clips
Hey clbeech,

Can you possibly export this .FLA in Flash MX(6) for me?

Or, simply just paste the actionscript here?

Thanks!!!! Best,
-joshua
• ###### 11. Re: Need to find nearest movie clips
you're using circles for movieclips and failing to understand the issue.
• ###### 12. Re: Need to find nearest movie clips
kglad: that was for clbeech, right?

-j
• ###### 13. Re: Need to find nearest movie clips
yes.

but it may apply to you because unless you have similar movieclip shapes (that are the same and small), his code will be of little help to you.
• ###### 14. Re: Need to find nearest movie clips
if you want the distance between registration points, then your task is simple. just calculate the distance between the movieclips.

p.s. don't send private messages when you won't accept a return message.
• ###### 15. Re: Need to find nearest movie clips
Thanks, but what's agood way to do the calculating?

Obviously, I could sort them easily if it was only one axis, but I need to account for X and Y.

Thanks again! Best,
-joshua

ps - sorry about PMs being blocked - I haven't used these forums in quite some time. -j
• ###### 16. Re: Need to find nearest movie clips
if your movieclips have instance names mc_1,...,mc_200 and you're using as2, you can use the following to find the closest movieclips to mc_1:

• ###### 17. Re: Need to find nearest movie clips

I really appreciate the help.

I don't want to look a gift horse in the mouth, but would there be a comparable AS1 set of functions? I'm still using Flash MX.

Thank you again,
-joshua
• ###### 18. Re: Need to find nearest movie clips
Thanks guys, I think I have all the info I need for now.

Thanks to all who helped. Gracias!!!

Kind regards,
-joshua
• ###### 19. Re: Need to find nearest movie clips
you're welcome.
• ###### 20. Re: Need to find nearest movie clips
@dot - you're welcome :) sorry I was away, and am not able to publish this down to MX, I'd used the colorTransfrom and Point classes and they are not supported, but kg's code is similar (better though ... as usual)

@kg - yeah, I understand your point, that the registration points do not necessarly reflect the closest bounds. This was just a simple method, and I'd been working on it previous to your first post anyway, so I just threw it out there. (you always beat me, you're the master :)
• ###### 21. Re: Need to find nearest movie clips
well, you were right about this one. the op's needs were simple with the distance between 2 movieclips being exactly the distance between the movieclip's registration points.