0

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?

2

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

what about this situation @MickMalone1983 ?<code> mc1.tx1.text ="";mc2.tx2.text="";...mc100.tx100.text="";</code> How can I do that by this[]?
Why is the text variable named differently inside each? In any case, in a loop I believe you could do the above with box notation this["mc"+i]["tx"+i].text = ""; ...but I don't see why you are doing this? Do you have 100 different text boxes inside each movie clip?
its a mistak that i did in my project!! :( Yes ! I have 100 100 different text boxes inside each movie clip
If your mc[i] are instances of the same MovieClip (as I assume they are) then they will all have the same children. If you change the name of tx? to txi in one, it will change in all. Call it, say, txt - you can then reference it in the loop I suggested with mc.txt.text = ""; ...or if you're still doing it the old way, this["mc"+i].txt.text = "";
Erm...this sounds like a mistake you should probably fix before you go on!
|