Home | Contact Us | Store

Access a Random Element of Array with Flash ActionScript 3

This Flash ActionScript tutorial shows how to access a random element of Array.

Your flash player version is too old to see this video file!

Flash Tutorial Content:

For some Flash games or Flash webpage design, we like to access a random element of an Array. This Flash ActionScript tutorial shows you how.

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"];

 

//Display all values of trayArray
var i:int;
for (i = 0; i < trayArray.length; i++) {

this["item" + (i + 1) + "_txt"].text = trayArray[i];

}

 

//Pick a random element from trayArray
output_txt.text = "Let's pick " + trayArray[Math.floor(Math.random() * 5)];

Download Flash Source File:

Flash Source File array-6.fla

Remarks:

This Flash ActionScript tutorial also shows how to access a random element of an Array. Flash Developers sometimes use external text database to store data. The text file will usually load to Array and the data can be accessed easily. The last Flash ActionScript Array tutorial will show you how to load an external text database file into an Array.

Flash ActionScript Tutorial