How to Reference Objects Dynamically

ActionScript 2.0

Sometimes it’s useful to reference a movie clip (or any object) by combining individual pieces of its instance name dynamically.  In coding up a version of Space Invaders, for example, you might employ a dozen instances of the same movie clip symbol to represent a dozen alien space ships.  Each clip will have its own instance name, of course, which might be alien0, alien1, alien2, and so on.  Rather than create an array to hold each instance name or reference, you might rather combine the string “alien” with the numbers 0, 1, 2, etc., to formulate your reference.  So, you might try something like this …

var alienShip;
for (var n=0; n<12; n++) {
  alienShip = "alien" + n;
  alienShip.gotoAndPlay("attack mode");
}

… because this should allow the variable alienShip to increment through each instance name — when n is 0, its value will be “alien0”; when n is 1, “alien1”; and so on — but it doesn’t work.  Why?

Well, this is where a good grasp on datatypes comes in handy and why I prefer the strongly typed syntax provided by ActionScript 2.0.  In the above example, the variable alienShip isn’t a movie clip reference at all:  it’s a string.  As the value of n increments, n concatenates with “alien” to form the string “alien0,” the string “alien1,” and so on.  Strings, of course, are not instances of the MovieClip class.  Look up the String class entry in the ActionScript Language Reference … you won’t find a String.gotoAndPlay() method.

We need a way to resolve a string to an object reference, and ActionScript provides two solutions:  the global function eval() and the array access operator.

eval() function

The eval() function accepts a string or series of strings and returns an actual object reference based on that expression.

If the actual instance name is alien0, then eval("alien0") will return the reference you need.  Same goes for eval("alien" + 0), eval("ali" + "en0"), and any other variation on that string, including eval("alien" + n)

var alienShip:MovieClip;
for (var n=0; n<12; n++) {
  alienShip = eval("alien" + n);
  alienShip.gotoAndPlay("attack mode");
}

Notice my strongly typed variable, established by the :MovieClip suffix.  ActionScript 2.0 is required for this syntax and provides a handful of benefits, one of them being improved error messages at compile time.  If, somewhere down the line, we attempt to set alienShip to something other than a MovieClip instance, the compiler will complain.  If nothing else, it’s a reminder that alienShip must refer to an actual MovieClip instance (not a string!) in order for the line alienShip.gotoAndPlay() to succeed.

Note:  From Flash 5 on, you cannot use eval() on the left side of an equation, such as …

eval("varString" + varNumber) = 55;
eval("varString") = "Tabasco® sauce";

If you find yourself in the above situation, you can either use the Flash 4-era set variable statement …

set("varString" + varNumber, 55);

… or the array access operator (see below).  Remember, this only matters if you’re trying to resolve an object reference on the left side of an equation.

array access operator

The array access operator, [], is primarily used to set and retrieve elements in an array; however, it can also be used to resolve object references on either side of an equation.  Rather than standard dot notation, where an object’s members are accessed via the dot operator …

myMovieClip._x

… the array access operator allows for the use of strings …

myMovieClip["_x"]

… just remove the dot and wrap the desired member in square brackets.  Since a MovieClip instance is nothing more than a property of its parent, you can use the global this property to access the movie clip itself …

var alienShip;
for (var n=0; n<12; n++) {
  alienShip = this["alien" + n];
  alienShip.gotoAndPlay("attack mode");
}

Leave a Reply