|
Part One:
Creating Arrays
Part Two: Using Arrays
Part Three: Manipulating
Arrays
Open a new flash file and, selecting the first frame
type the actionscript on the left hand side of fig.3
into the actionscript panel, test your movie and in
the output window you'll see the text in the right hand
side of fig.3.
The task is more elegantly achieved with the use of an
array. Type the actionscript on the left hand side of
fig.4 into the actionscripts panel. This script
comprises of firstly a literal array which is called ‘wordOutput’
and a reference to one of the array elements (inside
the ‘trace()’ statement). Test
your movie and in the output window you'll see the text
on the right hand side of fig.4. This is the most
important concept to remember about arrays: The index
number of an array begins at zero. In this array the
element at position ‘0’ is the string “This”,
the element at the ‘1’ position is the string
“is”, and so on.
If you modify the script in the actionscript panel to match
that on the left hand side of fig.5 and test your movie
you'll find the output window reads the same as it did
when you used the script from fig.3. Because an
array's indexing starts at zero, despite the fact
that there are ten elements in the ‘wordOutput’
array, the index number of the last element is ‘9’.
fig.5 uses an array, but saves no labour.
The correct way to make use of the array under these circumstances
is to move through the array one element at a time,
tracing the content along the way. We do it using a
‘for’ loop as shown in fig.6.
A ‘for’ loop is an incredibly
useful piece of script and well worth taking the time to
understand. It's structure is as follows: The
first line, ‘for(i=0; i<wordOutput.length;
i++){’ says, in layman's terms, create
a variable named ‘i’ and set it to zero,
do everything inside the curly brackets as long as ‘i’
is less than the length of the array named ‘wordOutput’,
once you've done everything inside the curly brackets
add one to the value of ‘i’ and as long as ‘i’
is still less than the length of the array named ‘wordOutput’
go through the loop again.
The ‘trace()’ statement
in fig.6 is almost identical to the ‘trace()’
statement in fig.4 and fig.5 with one important
exception. We are using the variable ‘i’
which will increment by one each time through the loop to
provide the index number of the element we want to trace
in the ‘wordOutput’ array. The first time
through the loop the trace statement will be applied to
the array element at the zero position, the second time
through the trace statement will be applied to the array
element at the one position, and so on and so forth.
Next
|