Flash ActionScript Tutorial: Assign Values to Array (2)
This Flash ActionScript tutorial also shows how to create new Arrays and how to assign values to Arrays but with a different method.
Flash Tutorial Content:
This Flash ActionScript tutorial uses a different method to assign values to an Array. We will create an empty Array at first. Then values are assigned to the Array later on.
The Flash Movie of this tutorial is shown as above.
Flash ActionScript Codes:
//Create an empty Array with the name trayArray
//You may imagine an Array like a DIY ice cube tray
//You don't need to decide the size at this moment
//You can assembly any number of ice cavity for your ice cube tray later
var trayArray:Array = new Array();
//We are going to make some ice cubes with different tastes
//Five different juices (values) are put into the Array
//Therefore the size of this Array is now 5
//Since this is a DIY ice cub tray, the size can be changed anytime.
trayArray = ["papaya juice", "orange juice", "apple juice", "milk", "coke"];
//The individual elements in the Array can be retrieved
//with reference to the element index
//Each element has an element index
//The element index of the first element is 0
//The element index of the second element is 1
//etc...
item1_txt.text = trayArray[0];
item2_txt.text = trayArray[1];
item3_txt.text = trayArray[2];
item4_txt.text = trayArray[3];
item5_txt.text = trayArray[4];
//Check the size of testArray
output_txt.text = "The size of trayArray are: " + trayArray.length;
Download Flash Source File:
Remarks:
This Flash ActionScript tutorial also shows how to create and assign values to Array. As you noticed that the values can be retrieved or accessed by using the element index. We can use a much easier for loop to retrieved the values of the Array. The next Flash Actionscript tutorial will show you how to do that.