Pregunta

I have four Movieclips inside four movieclip containers, and a filter called "myShadowFilter", like so:

option1BlueBox

is a movieclip inside

option1Container

and

option2Container

is a movieclip inside

option2BlueBox

and so on. I want to do this:

option1Container.option1BlueBox.filters = [myShadowFilter]; //line1
option2Container.option3BlueBox.filters = [myShadowFilter]; //line2
option3Container.option4BlueBox.filters = [myShadowFilter]; //line3
option4Container.option5BlueBox.filters = [myShadowFilter]; //line4

except with a loop, becuase I'm probably going to add more containers, each with a movie clip inside. A sudo-code of what I want to do is:

var containers:int = 1;
for (var i:int = 1; i<containers + 1, i++) {
'option' + i + 'Container.option' + i + 'BlueBox.filters = [myShadowFilter];';
}

Basically, I just want one loop which will run all the 4 commands. How do I make it work? It is giving me errors (as I expected) saying that there are syntax errors and colons are expected.

¿Fue útil?

Solución

Use the Array access operator. For more details, see this.

var containers:int = 1;
for (var i:int = 1; i<containers + 1, i++) {
    this['option' + i + 'Container']['option' + i + 'BlueBox'].filters = [myShadowFilter];
}

You could probably simplify this further by giving the blue box clip the same instance name (it only needs to be unique within the current scope). Then you could do something like this:

// Create an array of containers
var containers = [
    option1Container,
    option2Container,
    option3Container,
    option4Container
];

// Loop through each container and apply the filter 
// to the blue box container clip
for (var i = 0; i < containers.length; i ++)
{
    containers[i].option1BlueBox.filters = [myShadowFilter];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top