AS3 If Statements: If, else if, and else breakdown

if (condition1) {
method;
} else if (condition2) {
method;
} else {
method;
}

//Conditions set by the programmer
//An "else if" will test to see if another condition is met
//If condition is met the methods within the curly braces will be performed
//Notice that "else" does not need a condition

Actionscript:

if (bButton.label==" Turn On" ) {
bButton.label=" Turn Off" ;
mcBulb.visible=true;

} else if (bButton.label==" Turn Off" ) {
bButton.label=" Turn On" ;
}

AS3 If Statement: Turn a movie clip on/off with one button

Create a button component on your stage.
Give it an instance name of bSwitch.

Create a movieClip on your stage.
Give it an instance name of mcBulb.

Actionscript:

bSwitch.addEventListener(MouseEvent.CLICK, fSwitch);

bSwitch.label=" Turn On" ;
mcBulb.visible=false;
mcBulb.mouseEnabled=false;

function fSwitch(event:MouseEvent):void {
if (bSwitch.label==" Turn On" ){
bSwitch.label=" Turn Off" ;
mcBulb.visible=true;

}else if(bSwitch.label==" Turn Off" ){
bSwitch.label=" Turn On" ;
mcBulb.visible=false;

}

}

Example:

Get Adobe Flash player

AS3 Button Component: Create a toggle button and change color

Create a button component on your stage.
Give it an instance name of bRed.

Create a dynamic text box on your stage.
Give it an instance name of tMessages.

Actionscript:

//Sets the text at runtime
tMessages.text=" Pick your favorite color" ;

//Labels the button at runtime
bRed.label = " Turn Red" ;
//sets the toggle mode
bRed.toggle = true;

//Click listener
bRed.addEventListener(MouseEvent.CLICK, fRed);

function fRed(event:MouseEvent):void {
trace(event.target.selected);
//Checks to see if button is toggled down
if (event.target.selected==true) {
//changes the text color
tMessages.textColor = 0xFF0000;
//changes the button label
event.target.label = " Turn White" ;
}else{
tMessages.textColor = 0xFFFFFF;
event.target.label = " Turn Red" ;
}
}

Example:

Get Adobe Flash player