Learn Flash: Array

Home • Members Tutorials Forum iSnapChat Contact Us 


Flash Tutorials


Flash Tutorial - Intro to Arrays


Free Flash Tutorial



Intro   <<      Intro   A   B   C   D     Next   >>      



An Array is just a computer term for a: List. Arrays are extremely useful in Flash, and there are numerous uses for Arrays in other programs. The aim of this tutorial is to learn how to use Arrays. There are four examples in this tutorial that show you how to use Arrays in slightly different ways. This tutorial goes through all of them in detail, explaining all the ActionScript.


Example: Download the Flash file  Int 130a


In this case, the Array lists the images that are loaded into the Movie.

Cross Ref : The above Movie is an example from one of the next tutorials that uses Arrays to load external images: Loading External Images or Using Flash MX 2004 Components to Load External Images

Understanding how Arrays work

What is an Array? Think of an Array as a list of cells, each cell in the list has a number to identify it. Each cell can be used to hold information such as text, or a number, or an image or even some ActionScript etc.

Note: An Array size or length it is the same thing. It refers to the number of cells that store information in the Array.

Two Different ways to set up an Array:

I would personally recommend this method, it's easier to understand, edit, and control. You can clearly see which cell number each value is being stored into and then being added to the Array. Also it is simple and much tidier:

var myArrayName = new Array();
myArrayName[0] = "You can have text or a number here or anything you like really!";
myArrayName[1] = "text here";
myArrayName[2] = "123";
myArrayName[3] = "text and numbers 123";

This is another method, but I find it difficult to use. It's hard to know if you're entering a new value in the first Array cell, or the second or third or etc... It's easy to get confused, as this method doesn't display what cell number each value is going into:

var myArrayName = new Array("You can have text or a number here or anything you like really!","text here","123","text and numbers 123"); //  etc...

To get a value from the Array use:

myArrayName[x]; //Where x is the value from the Array you want to fetch. An example is:


This would return the first value from the Array which is: "You can have text or a number here or anything you like really!"

trace(myArrayName[1]); //Would return the second value in the Array which is: "text here"

Counting the Length of an Array

Notice how the Array starts at 0 and not at 1. This is important to remember as the count starts from zero and not one. An example of this important fact would be:


This would tell you that the Array length is equal to 4. Yes there are 4 items listed in the Array but the last number of the Array is 3. Is Flash inaccurate? No, Flash is accurate because the computer counts from 0. It treats 0 as the first number instead of 1. Always remember this!.

To overcome this problem just minus 1 from the Array, and you will find the highest value in the Array:

trace(myArrayName.length-1); //This will return the Array length as 3 and 3 is the highest value in the list.

An example of when you may find this problem would something like this (for the moment don't worry if you don't understand the ActionScript). What is important is that you set an Array to have a length of 3:

var myArrayName = new Array(3);
for (var i = 0; i<=myArrayName.length-1; i++) {
myArrayName[i] = "myImage"+i+".gif";

If you place the above code in Frame 1 of an empty Flash Movie you will notice that when you test the Movie it will display the following in the Output Box:

The Image names are traced in the Output Box when you test the Movie.

Although the Array length above was set to 3, it starts at cell number 0 and ends at cell number 2 not 3. For example, people often think if they set the Array to 3, then the end cell number will be 3, but it's always one less than the actual number you enter as the Array length. It's important to remember this and take it into account. The Array can be any length you like, just change the number 3 to whatever number you want.

You can change a value in Array, or add a new Array value at anytime simply by referring to:

_root.myArrayName[x] = "change current value or add new value";

An example of changing the first Array value:

_root.myArrayName[0] = "I have changed";

An example of adding a value to the Array:

_root.myArrayName[4] = "I have been added";

You can also set the Array size like this:

var myArrayName = new Array(3);

Then add your values to it later on. Although the Array length above is set at 3, you can still add values to the Array and increase the length:

myArrayName[4] = "myImage0.gif";

This would add "myImage0.gif" to cell 4, even though the Array was originally only 3 cells long. The Array size/length will now have changed from 3 to 4. That's all the basic's your need to know about Arrays.

Examples A to D

In this tutorial there are four examples of how to create and use an Array that lists different Strings (paragraphs or words) of text.

Cross Ref: In the next two tutorials there are more complex examples of how to use Arrays that load external images into your Flash Movie: Loading External Images or Using Flash MX 2004 Components to Load External Images


Intro   <<      Intro   A   B   C   D     Next   >>      

140704 visitors to this page since Nov 04 •

Phil Schulz's Facebook Profile
Webwasp is Phil Schulz's baby. You are welcome to contact me or become my Facebook friend: Click here

 Top of Page HomeMembers Tutorials Forum iSnapChat Contact Us 
 All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.