MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH Manual page 95

Learning actionscript 2.0 in flash
Table of Contents

Advertisement

To pass an object by reference:
1.
Select File > New and then select Flash Document to create a new FLA file, and save it as
copybyref.fla.
2.
Select Frame 1 of the Timeline, and type the following code into the Actions panel:
var myArray:Array = new Array("tom", "josie");
var newArray:Array = myArray;
myArray[1] = "jack";
trace(myArray); // tom,jack
trace(newArray); // tom,jack
3.
Select Control > Test Movie to test the ActionScript.
This ActionScript creates an Array object called
create the variable
second element of
statement sends
trace()
Flash uses a zero-based index, which means that 0 is the first item in the array, 1 is
the second, and so on.
In the following example,
by reference. The function
zeroArray()
parameter and sets all the elements of that array to 0. It can modify the array because the array
is passed by reference.
To pass an array by reference:
1.
Select File > New and then select Flash Document to create a new FLA file, and save it as
arraybyref.fla.
2.
Add the following ActionScript to Frame 1 of the Timeline:
function zeroArray (theArr:Array):Void {
var i:Number;
for (i = 0; i < theArr.length; i++) {
theArr[i] = 0;
}
}
var myArr:Array = new Array();
myArr[0] = 1;
myArr[1] = 2;
myArr[2] = 3;
trace(myArr); // 1,2,3
zeroArray(myArr);
trace(myArr); // 0,0,0
and pass a reference to
newArray
to
, it affects every variable with a reference to it. The
myArray
jack
to the Output panel.
tom,jack
contains an Array object, so you pass the array to function
myArray
zeroArray()
that has two elements. You
myArray
. When you change the
myArray
accepts an Array object as a
About variables
95

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?

This manual is also suitable for:

Flash 8

Table of Contents