This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org. [as]quote1 = "Flash is cool!";
quote2 = "Flash is my favourite program";
quote3 = "Flash rules";[/as]
However a better way may be to use an array, there are numerous ways of creating an array but i will show you the easiest way first:
[as]name_of_array = new Array();[/as]
So to store our quotes in an array we could use:
[as]quotes = new Array();[/as]
Ok thats great we now have an array, but the problem is we dont have any data inside of our array, thats not very useful so we will now place data inside of our array, we do this by using an index number, the index number refers to the position in the array.
To place a piece of data inside the first element of our array we would use:
[as]quotes[0] = "Flash is cool!";[/as]
As i mentioned before, arrays in flash 5 are zero based, meaning that the first element in the array has an index of zero(0).
So to place a piece of data inside the second element of our array we would use:
[as]quotes[1] = "Flash is my favourite program";[/as]
We use this syntax to fill an element of an array with a piece of data:
[as]name_of_array[index] = value;[/as]
where name_of_array is the name of your array, the index is the position in your array and the value is the piece of data you want to add to your array.
So the code we created earlier using variables:
[as]quote1 = "Flash is cool!";
quote2 = "Flash is my favourite program";
quote3 = "Flash rules";[/as]
can also be written as follows using arrays:
[as]quotes=new Array();
quotes[0] = "Flash is cool!";
quotes[1] = "Flash is my favourite program";
quotes[2] = "Flash rules";[/as]
Thats all great, but that is still alot of typing, we can also create an array of values using this syntax:
[as]name_of_array = new Array("value1","value2","value3");[/as]
So basically the same as the code we wrote earlier, but in one long line of code, value1 will be the first element of your array, value2 the second element of your array and so on....
[as]quotes = new Array("Flash is cool!","Flash is my favourite program","Flash rules");[/as]
Thats brilliant we now know how to create arrays and what they can be used for, but there is one more way of creating arrays that i want to make you aware of, the syntax is as follows:[as]name_of_array = ["value1","value2","value3"];[/as]
Again similar to the previous syntax except that we didnt have to use the array object initilaizer(new Array())[as]quotes = ["Flash is cool!","Flash is my favourite program","Flash rules"];[/as]
The way you create arrays is entirely up to you but i prefer using the last synatx i showed you as it requires less typing.[as]mynewvariable = name_of_array[index];[/as]
So lets say we want to get the value of the first element in our quotes array and place it in a new variable, we would use:[as]firstquote = quotes[0];[/as]
the new variable would be called firstquote and its value would be Flash is cool![as]myarraylength = name_of_array.length;[/as]
the above code will create a new variable called myarraylength and its value will be the number of elements in the array you specify(name_of_array).[as]trace (quotes.length);[/as]
The output window should display three(3) if you have used the code i have given you previously.[as]for(z=0; z<name_of_array.length; ++z){
trace(name_of_array[z])
}[/as]
[as]z = 0;[/as]
We then say loop the following code while the value of the variable i is less than the length of our array:[as]z<name_of_array.length;[/as]
And then we say add one to the value of our variable i:[as]++z[/as]
So lets put this script into use, by printing out all the value of our quotes array:[as]for(z=0;z<quotes.length;++z){
trace(quotes[z])
}[/as]
[as]trace(quotes[z]);[/as]
will loop three times, this is due to the fact that the length of our quotes array is three(3), each time the code loops it will print out the value of the current element:[as]quotes[z][/as]
This is because we increment(add one) to the value of z each time the script loops.[as]mycars=new Array();
mycars[0] = "peugoet 306";
mycars[1] = "citreon saxo";
mycars[2] = "ford fiesta";[/as]
[as]mycars = new Array();
mycars["fast"] = "peugoet 306";
mycars["sporty"] = "citreon saxo";
mycars["old"] = "ford fiesta";[/as]
[as]mysportycar = mycars["sporty"];[/as]
That will create a variable called mysportycar and its value will be citreon saxo.