Subscribe to RSS
get email updates
home | about | pixDif AIR app | video tutorials
polyGeek.com

place your ad here

Web Premium



Get Qwest High Speed Internet



Finding a MovieClip’s indexNumber

December 27th, 2006 . by polyGeek

Suppose you use a loop and the createEmptyMovieClip method to create some MovieClips like so:

If you ran this code and hit Ctrl+L you would see that you have created 5 empty MovieClips.

Level #0: Frame=1
Movie Clip: Frame=0 Target=”_level0.clip_0″
Movie Clip: Frame=0 Target=”_level0.clip_1″
Movie Clip: Frame=0 Target=”_level0.clip_2″
Movie Clip: Frame=0 Target=”_level0.clip_3″
Movie Clip: Frame=0 Target=”_level0.clip_4″

Presumably you would then attach some assets or load something into these clips.

In such instances it’s pretty common that you will write code to deal with user interactions with these clips. If so you’ll need to know which clip is being interacted with. It’s probably best to dynamically create variables on the clip that will help you identify it but you know what? You already have. You have the MovieClips name clip_#. So what you really need to know is what is the index number at the end of the MovieClips’ name.

That’s easy enough to find using the split(“_”) method as such:

this._name.split(“_”)[1];

That works well enough assuming you used an underscore during the creation of the MovieClips. What if you used a hyphen or even no separator at all?

I’ve had to deal with this often enough that I created a utility class to deal with it. Here’s the general code removed from the class that you can use or you can download the Find class which has this method plus a lot of others for helping you find MovieClips by size, _alpha, position, etc.



Note that this function returns a Number not a String. So if you have a clip named clip_01 then what you will get back by calling mcIndex(clip_01) is the Number 1.

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

4 Responses to “Finding a MovieClip’s indexNumber”


comment number 1 by: Keith Peters

I hate to say it, but that’s a really overly complex way of doing it. Just assign an index variable at creation time:

this.createEmptyMovieClip(”clip_” + i, i, {index:i});

then you can get the index as a direct property.

trace(someMc.index);

comment number 2 by: polyGeek

@Keith, Thanks for pointing that out. You’re absolutely right. Many of the developers I work with aren’t really comfortable with that approach. It seems simple enough to me, and you, but many developers out there are confused enough by scope issues and such but they have a handle on instance names. So I created this utility for them.
In my code I would do something like this:

var mc:MovieClip = this.createEmptyMovieClip(“clip_” + i, i);
mc.index = i;
mc.onRelease = function…

Oddly, I know how to use the notation that you suggested but I never do. It just doesn’t seem as readable as my approach. To each his own.

comment number 3 by: Keith Peters

Yeah, in practice, I’d actually do it the way you wrote it. I’m not a big fan of the initialization object. Not very readable.

comment number 4 by: Randy

I think it is better to use events to deal with movieclip interactions. So if a movieclip is interacted with it broadcasts the event and when you capture the event you will have a reference to said movieclip. But on second thought, if a developer has trouble with scope, event handling may not be the best idea. (apparently I cant add ;)

   Welcome back (Change)

Leave a Reply

comment feed RSS   subscribe to this comment thread

Recent Posts

   



polyGeek.com

© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites
Local Directory for Los Angeles, CA

Better Tag Cloud