Flash Tip: Referencing MovieClip Instances using Variables
Flash Tips Overview
Over the years, there have been little ‘oh, duh’ incidents that have helped me and my team get through troubleshooting Flash projects. I’ve been meaning to create an archive of these incidents to hopefully help some others. While I can’t guarantee that these examples follow “programming principles”, they have gotten us through our projects? and we’ve learned quite a bit along the way. Feel free to send feedback so I can revise or even provide other “alternatives” to these problems/scenarios.
Referencing MovieClip Instances using Variables
For Actionscript Experience Level: Beginner
I’ve been asked a number of times, “How can I refer to an instance name using a string and modify the properties of the instance?” Most of the time, it’s because the author has several instances on the stage named in a sequence such as foo1, foo2, foo3, and wants to be able to refer to one of the movieclips using two variables (”foo” + 2 = foo2) like this:
var mcNum:Number = 2;
var mcName:String = “foo” + mcNum;
mcName._alpha = 30; // change alpha of movieclip to 30 to show effect
This code will not do anything, because there is no movieclip instance name of mcName. If you did a trace on mcName (trace(mcName)), you’ll see an output of “foo2″ because you declared a string variable. Still with me? Okay, here’s how you can do it:
var mcNum:Number = 2;
var mcName:String = “foo” + mcNum;
this[mcName]._alpha = 30; // changes alpha of movieclip to 30 to show effect
So what’s happening here? “this” is a global property that references the path to where your movieclip lives, so if you did a trace(this) on the root stage, it’ll return “_level0″ in the output panel. The brackets next to “this” are essentially allowing you to access the array of “this” (in this case, _level0) which is where the instance name “foo1″ lives. this[mcName]._alpha is the same as this.foo2._alpha.
Of course, if you knew the path to where the movieclip lived, you could hardcode that path like so: _level0[mcName], but, what happens if you wanted to move this code around, maybe within another movieclip nested on the stage? You’d have to update that code, otherwise your code would break if you forgot about it. Programmers will almost always advise against hardcoding absolute paths to objects, whether you’re working in HTML, PHP, or Actionscript. It’s best to keep things modular unless you absolutely need to (pun intended)– the global property “this” will help with that.
// Jacob





March 27th, 2007 at 12:43 pm
Oh wow; I’ve been trying to figure this out for ages. Thanks so much.
July 16th, 2007 at 8:14 am
That’s been so frustrating, couldn’t ever figure it out. Thanks!!!
July 6th, 2008 at 7:33 pm
Oh. My.
You just changed my world forever, thank you! There are a lot of flash developers who’ve learnt on the fly, me included, and these things just aren’t known! Hello more dynamic coding!
July 10th, 2008 at 8:13 am
DUH! Thanks you so much! Lol, I would hug you if you were here! Really couldn’t come up with it myself ^^
October 14th, 2008 at 7:08 am
Thanks alot man.You solved a big problem of mine.
November 28th, 2008 at 6:25 pm
Lovely site… and thanks for that tip…. even after two years!
But I am having one problem.
var mcName:Number = 2 ;
var mcName:String = “foo” + mcName ;
Whatever I do to alter the number var tracing mcName always results in “foo2″.
I can never target foo1 or fooAnythingElse
I know it’s a longshot after 2 years but thanks anyway
November 30th, 2008 at 11:53 am
Hi Piotrowski,
I sent you an email on this - hope it helps.
January 5th, 2009 at 10:00 am
This works great for changing the parameters, but if I try
foo2.play()
it says Error: 1000 Unable to find FLV on server.
Any help with this would be appreciated, thanks.
July 10th, 2009 at 10:51 am
Wow, this was a great help! You wrote it very clearly for a beginner like myself!