Loop as3; function intersects; onEnterFrame

actionscript-3flashflash-cs4flash-cs5

On the stage I have a three kind of movieclips
Few static movieclips whose name is: mc1; mc2; mc3…. them are visible on stage
Few static movielips" othermc1; othermc2l othermc3… and them have "visible = false"
One movieclip whose is moving on the stage and his name is "slider"
I have function when mc "slider" intersects one of the rest of mc1, mc2… to turn visible othermc1, othermc2

var alreadyHandled:Boolean = false;

addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(e:Event):void
{
    if(mc1.getRect(this).intersects(slider.getRect(this)))
    {
        if(!alreadyHandled)
        {
            show1();
            alreadyHandled = true;
        }
    }
    else
    {
        alreadyHandled = false;
        no1();
    }

function show1():void
{
    othermc1.visible = true;
}
function no1():void
{
    othermc2.visible = false;
}

How can i use this code in loop?
Thanks for help

Best Answer

If you want loop through mc1, mc2, mc3,... create an Array of MovieClips:

var movieClips:Array = [mc1,mc2,mc3/*,other...*/];

Then use for to iterate over the array elements:

for(var i:int=0; i<movieClips.length; i++){
   //do some thing with movieClips[i]
}

This code may help you:

var mc:Array = [mc1,mc2,mc3/*,...*/];
var omc:Array = [othermc1,othermc2,othermc3/*,other...*/];
addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(e:Event):void
{
    for(var i:int=0; i<mc.length; i++)
        if(mc[i].getRect(this).intersects(slider.getRect(this)))
            showMovieClip(i); //or omc[i].visible=true;
        else
            hideMovieClip(i); //or omc[i].visible=false;
}
function showMovieClip(i:int):void
{
    omc[i].visible = true;
}
function hideMovieClip(i:int):void
{
    omc[i].visible = false;
}
Related Topic