Using Arrays in Action Script

Arrays in actionscript are collection of data of same data type. They are ordered information that can be manipulated and sorted. We will take a look at some of the simple functions in actionscript Arrays that can be used in manipulation.

Step 1: Open a new project

Open a new flash project of type ActionScript 3.0. All the manipulations that are described below are valid for arrays in ActionScript 3.0 only.

Opening a new project

Step 2: Declaration of an Array

Open the Actions window to write the code. First is the declaration and the initialization of an actionscript array. There are two ways in which it can be done.

Array Declaration

Step 3: Tracing an Array

Arrays can be traced. So add a trace() function to trace the elements in an array. When the movie is tested by pressing the CTRL+Enter key, the output window reads the elements present in the array.

Array tracing

Step 4: Trace Individual elements

Index of an array starts with 0 and individual elements of an array can be accessed using the index number. Let’s look at some basic manipulations available:
Individual elements of an array can be traced by mentioning the index number.

Array tracing of Individual elements

Step 5: Add elements

Individual elements can be added to the array. The easiest way is to type the array name followed by square braces that include the index number for the element to be added. This way the value will be added to the specified index. Let’s consider the array myArray1, it contains 3 elements till index number 2. So if we add an element at index 6, it creates empty slots for the other indices.

Array – Adding elements

Step 6: Use the push() function

When an element is added to the existing index number, the existing value is overwritten by the new value. Hence, this is not the preferred way of programmers. To add elements at the end of the array list, Push() function is preferred over the above said method.

Push() function

Step 7: Remove elements from an array

To remove elements from an array, Splice() is the function used. It takes in two parameters: the starting index and the number of elements to be deleted. If the number of elements is not specified, it deletes all the elements of the index from the starting index. Thus, when myArray1 is spliced from index1 till the next 2 objects, sample2 and sample3 are the elements removed from the Array. Pop() is another function that can be used to delete the last element of the array.

Splice() function

These are some functions used to add and delete elements from an array. There is a property that determines the length of the array. reverse() is a function that reverses the occurrence of the elements. The change made by the function is permanent. The elements are changed of their index number as well.