Script: Fading Menu

In this little tutorial you learn how to create a menu where submenues come in or disappear with fading using actionscript. In this file we create three different submenues, which are movieclips and give them names.

we set the visibility of all menues to false.
_root.square._visible=false;
_root.circle._visible=false;
_root.triangle._visible=false;

we define the variable i and set it to 0, since we need a status 0 and 1.
i=0;
we define the general function for the menues to fade in or out. Our object here
is the button we press to initiate the fade in and out. There is one
parameter, which is the menu name.
MovieClip.prototype.pressButton=function(menu){
    now we set the alpha value of the menues to 0, since this is
    required for the fade in function.
    _root.square._alpha=0;
    _root.circle._alpha=0;
    _root.triangle._alpha=0;
    when the button is pressed...
    this.onPress=function(){
        ...and i=1, which is not the case the first time a button is pressed,
        if(i==1){
            we define an interval function for the fade.
            myPause = new Object();
            myPause.interval = function() {
                prevMenu is a var, which will hold the menu name for the menu,
                which is just active. Here we fade out prevMenu to fade in the
                new menu.
                    if(prevMenu._alpha<=100){
                    prevMenu._alpha-=20;
                    if (prevMenu._alpha < 0){
                    prevMenu._alpha = 0;
                    prevMenu._visible=false;				
                    clearInterval(myTimer);
                }
                once prevMenu has alpha value of 0, we can fade in the new menu.
                if(prevMenu._alpha == 0){
                    fadeIn(menu);
                }
            }			
        }
        here is the interval parameter, which we can change.
        myTimer = setInterval( myPause, "interval", 100);
        if no button was so far pressed, we also fade in the menu.
        prevMenu here is not yet defined.
        }else if (i==0){
            fadeIn(menu);
        }
    }
}
this is the fade in function with menu name as a parameter.
function fadeIn(menu){
    we first set the menu visibility to true.
    menu._visible=true;
    then we fade in the menu.
    myPause = new Object();
    myPause.interval = function() {			
        if(menu._alpha<100-20){				
            menu._alpha+=20;
        }else {
            once done, we now define prevMenu and set it equal to the
            menu now shown on stage.
            menu._alpha = 100;
            prevMenu=menu;
            we also set i to 1 to execute the if statement in the button function.
            i=1;
            clearInterval(myTimer);
        }			
    }
    myTimer = setInterval( myPause, "interval", 100);
}
here we execute the button function for each button. Since this script
is within a movieclip, we have to refer to the menues with _root to go
back to the main timeline.
circleText.pressButton(_root.circle);
triangleText.pressButton(_root.triangle);
squareText.pressButton(_root.square);