Flash CS3 Tutorial – Arrays

0
13

An array is basically a container for holding data, similar to the way a variable hold data. The difference is that an array can hold multiple pieces of data. The location of each piece of data is referred to as its index and will always fall in a specific sequence. These indexes are numbers sequentially and begin at zero. Since Flash numbers each piece of data within the array it is easy for the programmer to access a specific piece of data at any given time. In ActionScript you are not forced to contain one type of data in an array. This means that an array may contain a number in index 0, a name in index 1, and a movie clip in index 2. Creating an array is rather simple an only requires two steps. The first is to declare the array, second is to populate the array with data.

Flash Array

Animation with Arrays

To Create an array select the first key frame on the actions layer in the time line. Copy the sample code. This code is pretty straight forward. You are creating a variable called colorsArray, data typing it as an array and setting it equal to a new array and that is it for creating the array. To add data to the array copy and past the code from the next example. With this code your are simply calling the name of the array, then in between the braces you are telling flash what index of the array to use. Lastly you are telling flash to put something in the specified index.

A loop will be used to access the data contained within the array, but first create another array by coping the example code. This is the section that makes this an advanced Flash CS3 Tutorial. The first line is the Array that this Flash CS3 Tutorial is all about. Next, is the for loop that you should already understand. Inside the loop is where the fun begins. The for loop is actually creating circles with a color from the first array and adding them to the cirArray so they can be used later. First, a variable num is created and set to be a random number between 0 an 4, (see math tutorials) if you need a better understanding. Next, the code is creating a new Sprite (object) and the next three lines are using graphics properties to create a circle (see controlling graphics tutorial) inside the Sprite or cir variable.

Once again add the example code to the actions panel. The first line of code is a simple function that is being called by the start button. Next, is the for loop that moves through the array. You could change the number 15 to be the length of the array by replacing it with cirArray.length and it would automatically detect the length of the array. Next, is an if statement checks to see if the object contained with the specific index of the array is on the stage. If the object is on the stage it moves the object 15 pixels and if it is not on the stage is moves it to the other side of the stage and randomly changes the y position. This simple if statement is what makes the 15 simple circles seem to be an endless amount.

Press Control-Enter and test the movie, if all goes well you have a Flash CS3 Animation created with ActionScript Arrays

 

 

Leave a Reply