Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

Access a Random Element of Array with Flash ActionScript 3

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

Please update flash player to view this Flash ActionScript tutorial!

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.