Arrays in ActionScript

Source: Internet
Author: User
Tags define array arrays implement numeric value variables reference
Array Difficulty:Intermediate
Software Environment :Flash 5


If you are interested in the array, you must know the variables. A variable is a container with data, which can be a number, a string, or a Boolean value.

An array is similar to a variable as a container for data, but it can also contain more data, and each element (part of the data) is attached to an index.

Arrays can be used to save your scripts and your organization, and they are often used to organize values that are associated in some way, using an index value
Separate from the other elements in the array. You can use the following method to define 3 variables:

quote1= "Flash is cool!"
Quote2= "Flash is I favourite program"
quote3= "Flash Rules"

Obviously there is a better way to implement the example above is to use an array. There are many ways to create an array, but the easiest way to do this is to first show:

name_of_array=new Array ()

So we're going to quote an array, and we'll use this:

quotes=new Array ()

Well, now that we have an array, the problem is that there's no data, it's useless, so we're going to put the data in our array now and we
Use an index number to define its position in the array.


We can put the first element in the array in the following way:

quotes[0]= "Flash is cool!"

I would say that in Flash 5 the index subscript is based on 0, meaning that the first element in the array has an index value of 0.
So we can put a data in the second element of our array:

quotes[1]= "Flash is I favourite program"

You can add all the elements in an array with the following syntax:

Name_of_array[index]=value

Description: The Name_of_array above is the name of your array, and index is where you want to add the data to your array.
So we created the code to use the variable earlier:

quote1= "Flash is cool!"
Quote2= "Flash is I favourite program"
quote3= "Flash Rules"


Can also be written like this:

quotes=new Array ()
Quotes[0]= "Flash is cool!"
Quotes[1]= "Flash is I favourite program"
quotes[2]= "Flash Rules"

We can also adopt the following syntax to create:

Name_of_array=new Array ("value1", "value2", "Value3")
This is implemented in one line of code, value1 is the first element in the array value2 is the second element in the array, and so on.
We use the following syntax to create the array we reference:

quotes=new Array ("Flash is cool!", "Flash are my favourite program", "Flash rules")

Now that we know how to create an array and how to use it, the following syntax can also be used to create a number sister:

name_of_array=["value1", "value2", "Value3"]

This differs from the previous one by not using an array pair image (new array ())
So we can create the referenced array code as follows:

quotes=["Flash is cool!", "Flash are my favourite program", "Flash rules"

I don't care how you do it, but I still like to use this last method to create an array, it's so simple.
Now we know how to create an array, but sometimes you want to access your array in a script, well fortunately there is another way to achieve it:

Mynewvariable=name_of_array[index]

So we want to go for a new variable. Value of the first element in the array please do this:

Firstquote=quotes[0]

The new variable value named Firstquote is the "Flash is cool!"

The implementation of arrays in scripts
Imagine if we wanted to get all the elements in the array, but we didn't know how many elements there were in the array.
Array sets a number of properties and methods, with a property of length that gives the number of elements in the array.

The syntax is as follows:

Myarraylength=name_of_array.length

The above code creates a new variable in which the value is the number of array-like (Name_of_array) elements you specify.
In the case where we refer to the array, we use the following code to get the number of elements in the array:
If quotes is an array, then
Len=quotes.lengthThe value of this Len variable is the length of the array.

The length of the array is equal to its highest index value plus 1. If the maximum index value is 2 then the length of the array is 3, which means it has
3 elements, because the array subscript in Flash 5 is based on 0.

Now that we know how to use the length property of an array, we can use the following script to draw the values of all the elements in a number of levels.

for (Z=0;z<name_of_array.length;++z) {
Write (Name_of_array[z])
}

Let's step through the code above.
A temporary variable Z was created first

z=0

The following code is a cyclic condition that loops when the variable z is less than the element length of the array

Z<name_of_array.length

Then make the increment of the variable z make it itself plus 1;
++z

You can put the code you need in here and they will be executed (name_of_array.length value) times

For (z=0;z<quotes.length;++z) {
...
}


Associative arrays:

Associative arrays can provide another way to store information that you can implement by using strings as subscripts instead of a number, making it easier to remember.
This inconvenience is not used in the loop because it is not a numeric value.

The definition of an associative array is the same as a workaround array, and the only difference is that it takes a string as the index subscript

Take a look at the example below, we define an array of cars:

mycars=new Array ()
mycars[0]= "Peugoet 306"
mycars[1]= "Citreon Saxo"
Mycars[2]= "Ford Fiesta"

This is the format of the associative array:

mycars=new Array ()
mycars["Fast"]= "Peugoet 306"
mycars["Sporty"]= "Citreon Saxo"
mycars["old"]= "Ford Fiesta"

It is sometimes useful to index values with characters, but it is very inconvenient to retrieve an array in a loop.

Now when you want to get the values in this type of array, you have to index the string, which is written in reference to the example above:

mysportycar=mycars["Sporty"]

The result is the variableMysportycaThe value is"Citreon Saxo"

If you have any questions, please contact the author directly! mailto:flashguru@flashkit.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.