AS3 Arrays: Access objects inside an array

Add five button components to the stage.

Give first button an instance name of bButton1.
Give second button an instance name of bButton2.
Give third button an instance name of bButton3.
Give fourth button an instance name of bButton4.
Give last button an instance name of bButton5.

Actionscript:

var aButtons:Array = new Array (bButton1, bButton2, bButton3, bButton4, bButton5);

trace(aButtons);
//returns the contents of the array
trace(aButtons[1].name);
//trace the name of object in specific position in an array. Index position must be in brackets. This trace would return "bButton2".
trace(aButtons[1].label);
//trace the label of object in specific position in an array. Index position must be in brackets. This trace would return the label of your second button.
trace(aButtons.length);
//returns the length of the array. This trace would return "5"

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

URLRequest: Make an Email link using AS3 (Option 2)

Create a button movieclip on your stage.
Give it an instance name of bEmail.

Actionscript:

bEmail.addEventListener(MouseEvent.CLICK,fEmailClick);

function fEmailClick(event:MouseEvent):void {
navigateToURL(new URLRequest("mailto:dankempton@gmail.com"),"_self" );
}

Example:

Get Adobe Flash player

URLRequest: Make an Email link using AS3 (Option 1)

Create a button movieclip on your stage.
Give it an instance name of bEmail.

Actionscript:

var vEmail:URLRequest = new URLRequest(" mailto:dankempton@gmail.com" );

bEmail.addEventListener(MouseEvent.CLICK,fEmailClick);

function fEmailClick(event:MouseEvent):void {
navigateToURL(vEmail," _self" ) ;
}

Example:

Get Adobe Flash player

Target an Event: Have a function detect which target is triggered

Create two button movieclips on your stage.
Give one an instance name of bButton1 and the other bButton2.

Add this code to your actions:

bButton1.addEventListener(MouseEvent.ROLL_OVER,fOver);
bButton2.addEventListener(MouseEvent.ROLL_OVER,fOver);

function fOver(evt:Event):void {
tText.text="Over "+evt.target.name;
trace("Over "+evt.target.name);
}

EXAMPLE:

Get Adobe Flash player

addEventListener: Basic Button

There are many ways to create buttons. Here is an example of a simple EventListenter:

//Activates the "fDown"function
bButton.addEventListener(MouseEvent.MOUSE_DOWN, fDown);
//Activates the "fUp"function
bButton.addEventListener(MouseEvent.MOUSE_UP, fUp);

//activated by "MOUSE_DOWN" listener
function fDown(evt:MouseEvent):void {
//changes the message to "Mouse Down"
tMessage.text="Mouse Down";
}

//activated by "MOUSE_UP" listener
function fUp(evt:MouseEvent):void {
//Clears the message
tMessage.text="";
}

Example

Get Adobe Flash player