ActionScript Programming/PartI/Chapter 3/Arrays

Arrays
Arrays are almost the most useful data type of all others. This data type was designed for keeping not one but many values in it. For example you want to have 10 users’ names in variables. So you must declare 10 variables with different names. It will be hard for you do add another user. But using arrays you can just declare one variable and keep all 10 users’ names in it. Now we will learn how to use array and its methods.

The example above shows you the usage of array. Line 1 just declares an array named “users”. Line 2 declares an array with 10 elements. This means that in that array can be kept 10 values at indexes 0 through 9. The first element in an array is always 0. Line 3 declares an array and sets its’ first 5 elements values. “c” will be the element with the index 2 in this array, because as we have already said the index of the first element in an array is always 0. Now lets write a small program to understand arrays better. Write the code written below to Frame 1 actions.

In line 1 we declare array “users” which consists of 3 elements. In lines 3-5 we set “users” each element. If you have an array and you want to get or set any of its element then you must write the name of the array, then “[]” and the number of element inside the quotation marks (“[]”). For example in line 3 we set the first element of “users” to “User 1”. You can use arrays for storing any data in them. For example you have an array which consists only of 2 elements. You can set the first elements value a string (e.g. myArray[0] = “hello”) and the second element a number (e.g. myArray[1] = 5). And finally in the lines 8-11 we trace our “users” array. Now we will create a more complicated example. Follow the steps:

Step 1

Select the Text Tool from the Toolbox and draw Text. Then select it and in the Properties window set its’ width to 80, height to 25, font size to 20 and change the Text Type to “Static Text”. Type “Number:” in it.

Step 2

Select the Text Tool from the Toolbox and draw Text. Then select it and in the Properties window set its’ width to 30, height to 25, font size to 20, var to “input”, change the Text Type to “Input Text” and select “Show Border Around Text” button. Place this Text near the previous Text. This will be our input field.

Step 3

Select the Text Tool from the Toolbox and draw Text. Then select it and in the Properties window set its’ width to 110, height to 25, font size to 20, var to “output” and change the Text Type to “Dynamic Text”. Place this Text under the two previous labels. The scene must look like this.



Step 4

Set the Fill Color to green and the Stroke Color to black. Then select the Rectangle Tool in the Toolbox, click the “Round Rectangle Radius” button  in the Options window, set the “Corner Radius” to 5, press OK and draw a rectangle. Double click the rectangle you have drawn and set its width to 35 and height to 25. Select the Text Tool from the Toolbox and draw Text. Then select it and in the Properties window set its’ width to 25, height to 18, font size to 13, color to red and change the Text Type to “Static Text”. Type “OK” in it and place it in the center of the rectangle. Now select the whole rectangle and this text (by dragging mouse) and convert it to button. In the “Convert to Symbol” dialog set the button’s name to “button” and press OK. Select the button you have created, place it near the Input Text and in the Property window set its name to “btnOK”. The scene must look like this.



Step 5

This is enough for our design. Now open the Frame 1 actions and type the following in the Script Pane:

Then right click “btnOK” and choose “Actions” to view its’ actions. In the Script pane type the following:

I think you understood everything. The first part of code was nearly the same as in the previous example, and in the second part of code we write a code which must be performed when the user click our “btnOK”. In lines 1 and 3 is written the beginning and the end of “release” event, and in line 2 we write the element of the “users” array which the user has written as “input” is the text which the user has written in the Input Text and “output” is the Dynamic Text in which the element of array is written. The arrays can also be multidimensional. For example you have some users and their phone number and address. For not making 3 different arrays it is easier to create one multidimensional array. The example below shows how to do that.

In lines 1 to 3 we create a multidimensional array “users”. You can write all these lines in one line. The variable is declared in three lines because it is easier to read. If you combine these lines then the result will be a very long line. Why we don’t put “;” after each line? Because the lines 2 and 3 are the other parts of the main line. So by putting the “;” in the 3-rd line we tell the computer that this was the end. The program will think that this is only one line. Now lets understand how we create a multidimensional array. This is the type of writing multidimensional arrays:

var :Array = new Array( [, , <…> );

When you write “var myarray = new Array( [], [], [] );” you create an array with 3 subarrays. Imagine something like this:


 * Myarray
 * 0
 * value
 * 1
 * value
 * 2
 * value

If you write “var myarray:Array = new Array ( [ [], [] ], [], [ [] ] );” then this is something like this:


 * myarray
 * 0
 * 0
 * value
 * 1
 * value
 * 1
 * value
 * 2
 * 0
 * value

This is like there is a folder, in which there are three folders. In the first folder there are two folders, in the third folder there is one folder. So if you want to get value from the second folder of the first folder then you must write “myarray[0][1]”. We do the same in our example. If we draw the structure of our array then it will be:


 * users
 * 0
 * 0
 * User1
 * 1
 * Phone1
 * 2
 * Address1
 * 1
 * 0
 * User2
 * 1
 * Phone2
 * 2
 * Address2
 * 2
 * 0
 * User3
 * 1
 * Phone3
 * 2
 * Address3

In this structure you see that the names on folders are only numbers. You can also give them names by writing the following:

The output is 3.

« Previous &emsp;&emsp; Next »