Finding a MovieClip’s indexNumber
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.
















