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"

Leave a Reply