Reputation:
I have 100 movieclips in my stage named mc1,mc2,...,mc100 . I want to set their visibility to "false" by a for loop like this:
for ( var i:Int=1;i<=100;i++)
{
mc+i.visible=false;
}
How can I do that?
Upvotes: 0
Views: 1254
Reputation: 1054
You could try:
for (var i:int = 1; i < 101; i++)
{
this["mc"+i].visible=false;
}
This will work on both timeline and document class.
However, this is not very efficient. If you're going to be using this loop more than once, it's better to store references in an array and iterate over that, rather than use these lookups each time:
Use this at the very start of the application:
var objects:Array = [];
for (var i:int = 1; i < 101; i++)
{
objectsArray[i] = this["mc"+i];
}
Then, when you need to cycle later, use this loop:
for (i = 1; i < 101; i++)
{
var mc:MovieClip = objectsArray[i];
//Now, do what you need to this eg mc.visible = false;
}
Upvotes: 2