Ok, you might have wondered many times how to turn a string into code at runtime, or, if you're a sane person unlike me, you might not. In any case, there are situations where you pretty much have to in order to keep track of dynamically-generated objects.
For example, create a new .fla (AS2), make a new MovieClip called "b_mc" and on the frame insert this sample code:
var c:Number = 0;
b_mc.onPress=function():Void {
var clip:MovieClip = this.duplicateMovieClip("mc"+c++, _root.getNextHighestDepth());
clip.startDrag();
clip.onPress = startDrag;
clip.onMouseUp = stopDrag;
}
// Basically, it will allow you to drag a copy of your movieclip on the stage.
... Now, lets say that you have a function after this code that aims to remove the second MovieClip duplicate once any key is pressed.
You know from the duplicateMovieClip() that the second duplicate will be called "mc1" (remember the first is going to be "mc0").
Anyway, so how are you going to go about making mc1 disappear??
Well... The answer is using the an "Assotiative Array" that points to the parent of the MovieClip in evidence.
An associative array, is an array that references an elements by name(string) rather than by an index (number).
So, in our case, to access the "mc1" MovieClip, we would need to use:
_root["mc1"]
or even:
_root["mc"+1]
Flash automatically creates associative arrays for every Object and MovieClip attached to a timeline. Basically, by saying "_root[]" we are looking for an object in the _root[] that is called "mc1."
So here's the code:
var c:Number = 0;
b_mc.onPress = function():Void {
var clip:MovieClip = this.duplicateMovieClip("mc"+c++, _root.getNextHighestDepth());
clip.startDrag();
clip.onPress = startDrag;
clip.onMouseUp = stopDrag;
};
listener = new Object();
listener.onKeyDown = function():Void {
removeMovieClip(_root["mc"+1]);
};
Key.addListener(listener);
In some cases you would need to use this["childname"] or even mymovieclip["childname"].
Also, using this method, you can even call a method from a string:
e.g:
_this["methodname"]()
You need to have the "()" afterwards!
... I discovered that one by myself by the way.
THE END.
PS. This is one of the most important concepts in AS; one that defines intermediate devs from beginners.
Enjoy :)))