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.
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 ones X positions separately.
Anyways, here is one way to reference them:
private var mcArray:Array = new Array(); // DEFINE ARRAY FIRST
private function myfunction():void{
for (var i:int=0; i<2; i++) { // THE FOR LOOP
var movieClip:MovieClip = new MovieClip(); // TEMPORARY VARIABLE
addChild(movieClip);
mcArray[i] = movieClip; // here is where we add the movieClip references to the array for later use
}
}
Now, to use those references:
mcArray[0].METHOD_HERE;
or
for (var i:int=0; i<2; i++) {
mcArray[i].do_something = something;
}
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 something in those MovieClips I could use the mcArray to do so.
mcArray[0].graphics.beginFill(0x555555,1);
mcArray[0].graphics.drawRect(0,0,900,300);
mcArray[0].graphics.endFill();
or loop through the array and so something (like applying an animation to each and every mc in the Array
simple as that.
13 responses so far ↓
Fin // April 27, 2008 at 12:21 am |
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!
ryu619 // May 10, 2008 at 4:58 pm |
Very helpful. Thanks.
AS3– Creating TextFields in a loop. Also with dynamic variable names « The Great Magnet // May 14, 2008 at 4:43 pm |
[...] the way, lets use our little trick we learned about assigning dynamic variable names to movieclips here. The same issue applies. We want access to our textfields in case we want to modify them [...]
Jerome // September 19, 2008 at 1:47 am |
all the code in one block would be nice.
With parts of code like this, i could not recreate this script in flash cs3.
Vikky // October 6, 2008 at 6:15 am |
WOW YOU ARE THE MAN…
AT LAST I CAN SLEEP PEACEFULLY TONIGHT
THANK YOU VERY MUCH…
CHEEEEEEEEEEEEEEEEEERS
Rob // October 21, 2008 at 6:13 pm |
Thank you, that helps a lot!
Ronald // January 9, 2009 at 8:05 am |
You saved my day.
jason // February 11, 2009 at 3:34 am |
Why do you put addChild there? I don’t know ActionScript but I would have figured to do it like this:
mcArray[i] = movieClip;
addChild(mcArray[i]);
I would have thought to do it like this:
jef // July 10, 2009 at 4:08 pm |
thank you! I was going insane trying to figure this out coming from AS2. It forms the basis for everything so thank you.
tom // July 23, 2009 at 9:51 pm |
This seems also a possible way to be able to set to null objects removed from the display list created within loops? – as in:
removeChild(mcArray[0]);
mcArray[0]=null;
or is there a simpler way?
saintist // October 27, 2009 at 9:48 pm |
veri nice, i find this, thank, you save my time
saintist // October 27, 2009 at 9:48 pm |
veri nice, i find this, thank, you save my work time
pablo // November 6, 2009 at 3:57 pm |
Thank you so much. I couldn“t figure out how to do it.