Bits & Bytes

Declaring and Using Arrays in Actionscript 3.0

Arrays in Actionscript are extremely flexible; there are many ways to declare and assign values to an array in Actionscript. We can use functions, bracket notation, or constructors to create an Array object. We also have several ways to fill the entries of an Array object.

It is important to note that an Array in Actionscript is an object. So, an Actionscript Array is very different from say an array in C/C++. In fact, Arraysin Actionscript can hold many different types of data, which is different from arrays in most languages. However, Actionscript does also have a Vector type, which is similar to a traditional array in that, for efficiency, it is limited to holding only one type of data.

One dimensional Arrays are pretty straight-forward in how they can be declared and used. However, there are multiple methods for creating one-dimensional Arrays, which can be put together in numerous combinations to created multi-dimensional Array objects.

One-Dimensional Arrays

Function Notation

  1. var aMyArray:Array = Array();
  2. var aMyArray:Array = Array(3);
  3. var aMyArray:Array = Array(qMyObject);

Bracket Notation

  1. var aMyArray:Array = [“”, 17];

Contructor Notation

  1. var aMyArray:Array = new Array();
  2. var aMyArray:Array = new Array(3);
  3. var aMyArray:Array = new Array(1, 2, 3);

Above, we show several methods for creating an Array. The first three examples use function notation to create an Array of 0, 3, and 1 objects, respectively. Next, we have an Array of two elements that is created via the bracket notation; notice that the Array contains elements of different types: a string and an int. Lastly, we have three Array objects that are created using constructors: the first creates an Array with 0 elements, the second an Array with three elements, and the third an Array of the three elements 1, 2, and 3.

Filling an Array

  1. var aMyArray:Array = new Array();
    aMyArray.push(80, 120, 200, 440);
  2. var aMyArray:Array = new Array(4);
    aMyArray[0] = 80;
    aMyArray[1] = 120;
    aMyArray[2] = 200;
    aMyArray[3] = 440;

Above, we have two examples of how to declare and fill an Array with the four elements 80, 120, 200, and 400. In the first case, the push() function puts an element, or multiple elements, into an Array and extends the size of the Array. In the second case, we use the typical bracket operator [] to set the value of entries that are already allocated. There are many other functions available for removing elements, concatenating Arrays, and altering Arrays in many other different ways, as well; we will cover those later.

Multi-dimensional Arrays

  1. var aRow1:Array = new Array(1, 2, 3);
    var aRow2:Array = new Array(4, 5, 6);
    var aMyArray:Array = new Array(aRow1, aRow2);
  2. var aRow1:Array = [1, 2, 3];
    var aRow2:Array = [4, 5, 6];
    var aMyArray:Array = [aRow1, aRow2];
  3. var aMyArray:Array = new Array(2);
    aMyArray[0] = new Array(1, 2, 3);
    aMyArray[1] = new Array(4, 5, 6);
  4. var aMyArray:Array = new Array(2);
    aMyArray[0] = [1, 2, 3];
    aMyArray[1] = [4, 5, 6];
  5. var aMyArray:Array = [ [1, 2, 3], [4, 5, 6] ];

Here, we have 5 examples of how to create a two-dimensional Array object, by combining the methods for one-dimensional Arrays. We can continue like this and create Arrays with any number of dimensions.

Tags: , , , , , , , ,

Michael Hall

By: Michael Hall

One Response to “Declaring and Using Arrays in Actionscript 3.0”

  1. Frustrated says:

    Why can’t I just say, for example,

    var aMyArray:Array = new Array(2)(3);

    Why can’t Flash/ActionScript be a little more like Java?

Leave a Reply



© 2007–2022 LLC. All rights reserved.