Flash AS2 Tips for Designers, variables in target paths
How can I use a variable in a target path? This may be a well known bit of info for many Flash developers but for this designer who is not code jockey it was a quite a challenge. I have a flash file that has a movie clip (myclip) attached to it dozens of times, each clip is dynamically named (myclip1, myclip2, myclip3, etc.) and each contains a variable (var_in_myclip) which is loaded from xml. The root contains 2 variables, one is a number (var id:Number;) and the other is a string (var whichclipvar;).
So I need my “whichclipvar” to be the same as one of my “var_in_myclip” in one of “myclip” movie clips and I want to use my “id” number to target a specific “myclip” to get it’s var, like this
whichclipvar = _root.myclip1.var_in_myclip;
But I need the myclip1 in the target path to change if “id” changes.
The solution is to create a new variable that can be used as a string in the target path. Like this:
var myclippath:String=”myclip”+id;
Then use [myclippath] in place of the movie clip in your target path. The square brackets are important for this, and no preceding “.” who knew? The whole code looks like this and requires a movie clip on the stage with an instance name of “myclip” and its symbol properties must be set to export for action script and named “myclip”:
var id:Number;
//// NOTE: the id var can be set in any way that works for your purposes///
var whichclipvar;
var mycontent:XML = new XML();
mycontent.load(“my_content.xml”);
mycontent.ignoreWhite = true;
mycontent.onLoad = function (success) {
if (success) {
var myxml = this.firstChild;
for (var i=0; i<36; i++) {
myclip.attachMovie(“myclip”, “myclip”+i, (0+i), {var_in_myclip:myxml.firstChild.childNodes[5].childNodes[i].childNodes[1].firstChild.nodeValue,});
}
}
var myclippath:String=”myclip”+id;
whichclipvar = _root[myclippath].var_in_myclip;