This is already very well known, but if you’ve been on AS3 for a while you’ll notice that they have eliminate the instance name usage for AS3 (this, I agree is more practical for AS3 OOP’s structure). We can however, insert an instance name manually on stage, but what about the rest of us that are adding movieclips on stage programmatically?
Here’s what you might be attempting already:
var mc:MovieClip = new MovieClip();
mc.name = i;
addChild(mc);
}
And when you try to declare it (1.gotoAndPlay(2)), you can’t. I have yet to find a usage for the .name parameter (seriously, other than scene class, but in OOP you hardly use scene), but so far, it’s confirmed that it’s declaring it in such a way is not working by any means. Here’s what we could do to turn it around – by create an Array or Object to store your movieclip.
for (var i:uint=0; i < someNumber; i++) {
var mc:MovieClip = new MovieClip();
mcArr.push(mc);
addChild(mc);
}
We can now run the gotoAndPlay function by using mcArr[0].gotoAndPlay(2). This method also goes with any form of display, for example if it’s a textfield that is in the array, you could change it’s text by using mcArr[0].text = myString. What about trying to insert a mouse event for the movieclips? Surely we won’t use the no brainer way to declare mcArr[0].addEventListener(…) onto each and everyone of the movieclip.
Either that, or we can store another variable inside the movieclip so that we can read it from the listener later.
for (var i:uint = 0; i < someNumber; i++) {
var mc:MovieClip = new MovieClip();
var num:uint = i
mc.num = i;
mc.addEventListener(MouseEvent.MOUSE_DOWN, down)
mcArr.push(mc);
addChild(mc);
}
In order to put it in good use, your mouse event function could look like this:
trace(e.currentTarget.num);
mcArr[e.currentTarget.num].gotoAndPlay(2);
}
What the above script and function does is creating numerous movieclips, and each of them will gotoAndPlay(2) itself when clicked. This is just a tip of an iceberg, but it could turn into a pretty fancy xml gallery. To those professionals, I know this is a little bit ameteurish, but I’m sure someone or something out there will find this useful. And they could thank me.