Assigning dynamic variable names to objects created in a for loop in AS3.

You have to get very creative to search these questions on google because words like “for” and “loop” are so utterly common.

If you have a loop in AS3 creating multiple variables but need them to have unique names (for later modification) you need to create something like an array to store the references to the variables.

For example, anything you create in a for loop is a temporary reference. It disappears after the loop is complete, and whatever objects were created stick around in the displayObject.

In my situation I had a loop creating my navigation elements, but needed to position each X value separately.

Here is the method I used to keep the references to these variables:

private var mcArray:Array = new Array();  // array that holds references
private function myfunction():void{
	for (var i:int=0; i<2; i++) { 
		var movieClip:MovieClip = new MovieClip(); // the temporary variable
		mcArray[i] = movieClip; // store reference to temporary variable in array

Now, to use those references:

// or
// --
for (var i:int=0; i<2; i++) {
	mcArray[i].foo = bar;

To be absolutely clear, the above example creates two MovieClips according to the loop (while i < 2), and places each MovieClip in the mcArray array at index i (which we have defined as the integer counting the loop).

Now if I wanted to access those individual MovieClips I could use the mcArray to do so.


I could also loop through the array to apply an animation to each item. The possibilities are endless when you use this basic concept!

Generate 1000 triangles. Animate each one to a random color and rotation.

40 thoughts on “Assigning dynamic/unique variable names in a for loop in AS3

  1. Thanks so much for this. I’ve been trying to do this all night. I couldn’t work out how to refer back to the movieclips I’d made and putting them into an array was perfect!😀

  2. Why do you put addChild there? I don’t know ActionScript but I would have figured to do it like this:

    mcArray[i] = movieClip;

    I would have thought to do it like this:

  3. thank you! I was going insane trying to figure this out coming from AS2. It forms the basis for everything so thank you.

  4. This seems also a possible way to be able to set to null objects removed from the display list created within loops? – as in:

    or is there a simpler way?

  5. I have following path to some movieclips.
    this.vote1.v0.addEventListener(MouseEvent.CLICK, SaveVote);
    this.vote1.v1.addEventListener(MouseEvent.CLICK, SaveVote);
    this.vote1.v2.addEventListener(MouseEvent.CLICK, SaveVote);
    this.vote1.v3.addEventListener(MouseEvent.CLICK, SaveVote);
    this.vote1.v4.addEventListener(MouseEvent.CLICK, SaveVote);

    Who do i make this with a for loop?

  6. Thanks for the great tip, I found it very useful. How could I modify this so I can add a movieclip to a 2-d array? I’m trying to create a grid of tile movieclips and I want to be able to add another clip to coordinates within the grid. I’d imagine it would end with something like:

    Any help would be appreciated.

    1. You want to associate one MC to an array coordinate?

      I’m pretty rusty here, but how about..

      //define 5x5 2d array
      2dArray = new Array();
      2dWidth = 5;
      2dHeight = 5;

      for (var i:int=0; i<2dHeight.length; i++) {
      2dArray[i] = new Array();
      for (var ii:int=0; i<2dWidth.length; ii++) {
      2dArray[i][ii] = new Array();
      2dArray[i][ii]['mc'] = myMovieClip;

      //alter 0,0 coordinate
      2dArray[0][0]['href'] = 'store other arbitrary data on grid';
      2dArray[0][0]['mc'] = myMC;

      //access row 2, col 4 MC

  7. Amazing. Dynamic variable names were one of my favorite things about actionscript 2.0 and they seem much harder to do now. This is so useful.

  8. Wow this is so useful, I’ve been trying to figure this out for days, you’ve saved me life. Really.
    Thank you🙂

  9. Thank you very much for sharing this code. I have a question tho. How would I use this for something like the SoundChannel class?

    If I want to play each channel, I would end up doing something like this:

    channelArray[i] = soundArray[i].play;

    Which might actually assign a Sound object to channelArray.

  10. Good day! I just found your webpage: Assigning dynamic/unique
    variable names in a for loop in AS3 | Useful Stuff.
    when I was browsing It looks as though someone appreciated your blog so much they decided to bookmark it.
    I’ll unquestionably be returning here more often.

  11. One must practice the comic scripts beforehand at home.
    Think of this as a way to document your improvements over time, and eventually it will be a very effective PR
    tool. He hosted Comedy Central’s’The Man Show’ as well as
    the ubiquitous pseudo-porn for the sexually crippled, ‘Girls Gone Wild’, both solely and
    shamelessly for financial gain.

  12. This might be the best blog for everyone who wishes to
    be familiar with this topic. You already know much its practically not easy to
    argue along. Outstanding stuff, just great!

  13. Of download rust course the sports world simply couldn’t imagine a year, Black Ops 2Developer:
    TreyarchRelease Date: Nov. But, should access be limited in respect to their
    children to care for animals when they aim at your face
    or perhaps U. Video games have become very hard to find these cheat codes throughout
    the video game. This is to advertise effectively in a week after its release and flop financially – these

  14. If the eighty three percent of all types of videogames
    are their best to dayz download make a list of the quoted official.
    This only causes the child thinks less of an instrument stimulating enough?
    While there are a great way to copy and backup your Playstation 3 games.

  15. We stumbled over here different web address and thought I might check things out.
    I like what I see so now i am following you. Look forward to going over your web page repeatedly.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s