Search code examples
actionscript-3flash-cs6

AS3 Restore Alpha of Multiple Objects on Click


I'm creating a simple interactive Mr. Potato Head game using AS3 and have used the following code to make objects disappear on a click. This is an example of the code that handles the Nose. When the nose is clicked, it disappears. Everything is working as intended; however, I want to include a single button that when clicked will cause all of the pieces (arms, eyes, mouth, etc) to reappear.

Nose.addEventListener(MouseEvent.CLICK, mouseHandler4);

function mouseHandler4(event:MouseEvent):void
{
    Nose.removeEventListener(MouseEvent.CLICK, mouseHandler4);
    addEventListener(Event.ENTER_FRAME, fadeOut4);
}

function fadeOut4(event:Event):void
{
    var a:Number = Nose.alpha-0.05;
    if (a<=0)
    {
        a = 0;
        removeEventListener(Event.ENTER_FRAME, fadeOut4);
    }
    Nose.alpha = a;
}

Solution

  • I would make an array of all the clips you want to fade back in;

    var pieces:Array = [arms, eyes, mouth];
    

    then on click, assuming you make a fadeIn function similar to your fadeOut4

    for (var i:int = 0; i<pieces.length; i++){
      fadeIn(pieces[i]);
    }