Manipulating Array Contents

Manipulating Array Contents

Flash CS4 ActionScript 3.0 Tutorial


How to Manipulate Data within an Array Variable Using Flash ActionScript



Once you have loaded your Array variable with data, you may wish to move, change or replace the contents. There are multiple ways to alter array values.

In this example we will demonstrate using pop and shift to remove items from the Array. The push and unshift methods add Array contents and reverse changes the Array order.

For more information on Arrays, see topics, Arrays, Accessing Array Data and Loading an Array.


Step One

Open a new Flash document and name the default layer Actions.

Step Two

Option double-click (PC - Alt double-click) frame one of the Actions layer.

Step Three

Copy/Paste the following code to the ActionScript panel.

var colors:Array = new Array("Red", "Green", "Blue");

trace("Start: " + colors[0] + colors[1] + colors[2]);


trace("Reverse: " + colors[0] + colors[1] + colors[2]);


trace("Unshift: " + colors[0] + colors[1] + colors[2] + colors[3]);


trace("Push: " + colors[0] + colors[1] + colors[2] + colors[3] + colors[4]);


trace("Pop: " + colors[0] + colors[1] + colors[2] + colors[3] + colors[4]);


trace("Shift: " + colors[0] + colors[1] + colors[2] + colors[3] + colors[4]);

Step Four

Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter). The Array values are displayed in the Output panel.

Dissecting the Code

The first line of the code at Step Three establishes a new variable named colors. For the colors variable, Red is loaded in slot [0], Green is in slot [1] and Blue is loaded in slot [2].

Line two traces the initial values for slots [0], [1] and [2].

The next line of the code reverses the order of the Array contents. A trace statement shows you the results.

The same is true for the following blocks of code. Unshift adds an item at the beginning and push adds one at the end. Pop removes the last item and shift removes the contents of the first slot [0]. Once you remove the contents of slot [0], the contents from the upper slots move down a notch.

The extra Array locations were included in the final two blocks to show that once the contents was removed, the value for that slot became undefined.


Contribute Ad