Search code examples
actionscript-3flashonclickvisible

Array items check as3


I have 4 movieclips in an array

var tiles:Array = new Array("tile1","tile2","tile3","tile4");

Inside each one there is code to vanish when it's clicked with the mouse on the second frame.

this.visible = false;

From the main timeline is the control for the mouse click for each of the tiles (only the first one is displayed).

tile1.addEventListener(MouseEvent.CLICK, fl_);

function fl_(event:MouseEvent):void
{
    tile1.gotoAndStop(2);
}

How can I make it so when all the tiles in the array become invisible, flash to take action (such as to go to frame 5)?

Thank you!


Solution

  • I've looked at your .fla. Here are two ways to do what you'd like:

    On Your Main Timeline: (replace the current main timeline frame 1 code with the following)

    stop(); 
    
    //loop through every child of the `cont` container, and add the same click listener
    var i:int = cont.numChildren
    while(i--){
        var tile:MovieClip = cont.getChildAt(i) as MovieClip;
        if(tile){
           tile.addEventListener(MouseEvent.CLICK, tileClick, false,0,true);
        }
    }
    
    function tileClick(e:MouseEvent):void {
        //this gets a reference to one that was clicked
        var tile:MovieClip = e.currentTarget as MovieClip;
    
        tile.gotoAndStop(2);
    
        //loop through the tile array to see if any are still visible
        var i:int = cont.numChildren
        while(i--){
            tile = cont.getChildAt(i) as MovieClip;
            if(tile && tile.currentFrame == 1) return;
        }
    
        //if we got this far, all the tiles are hidden, lets go to frame 5.
        gotoAndStop(5);
    }
    

    If the above is intimidating and you would prefer to keep it like it was before, then this is all you'd have to do: (again, this code will replace your current main timeline frame 1 code)

    stop();
    
    cont.tile1.addEventListener(MouseEvent.CLICK, tileClick);
    cont.tile2.addEventListener(MouseEvent.CLICK, tileClick);
    cont.tile3.addEventListener(MouseEvent.CLICK, tileClick);
    cont.tile4.addEventListener(MouseEvent.CLICK, tileClick);
    
    function tileClick(e:MouseEvent):void {
        MovieClip(e.currentTarget).gotoAndStop(2);
        if(cont.tile1.currentFrame == 1) return;
        if(cont.tile2.currentFrame == 1) return;
        if(cont.tile3.currentFrame == 1) return;
        if(cont.tile4.currentFrame == 1) return;
    
        //if we got this far, all the tiles are hidden, lets go to frame 5.
        gotoAndStop(5);
    }