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

Leave a Reply