Articles
Flash ActionScript: Using variables within target paths
16 February 2007
This is something I only recently discovered, but it’s helped me a great deal and opened up a lot of possibilities. Most Flash developers are probably already aware of it. However, for those who aren’t — you’ll be happy to learn.
I’d like to say up front that I’m no Flash wizard. If those more knowledgeable than I discover any errors, please let me know!
Let’s say you have two maps that allow users to scale and zoom and explore and such, and you want access to them through a function like so:
function showMap1():Void {
this.mcMap1._visible = true;
}
function showMap2():Void {
this.mcMap2._visible = true;
}
Normally I would write two separate functions for something like this, but I always wondered whether or not it was possible to write one function and pass an argument that targeted the correct movie clip instance. Well, there is:
function showMap(whichMap):Void {
this["mcMap"+whichMap]._visible = true;
}
Now you can call one function from wherever and specify your target through an argument:
showMap(1);
Even though it’s probably incorrect, I often call arguments variables since they behave the same way inside of a function. But this works with plain ol’ variables as well:
var myMovieClip:String="mcMap1"; this[myMovieClip]._visible = true;
I don’t have a huge library of Flash books, but I’ve never seen this in any reference material or official website. If I remember right, I found it in a forum after lots of searching. So I felt it was up to me to share the secret. Enjoy!
