EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 95

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
About variables 95
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
myArray that has two elements. You
create the variable
newArray and pass a reference to myArray. When you change the
second element of
myArray to jack, it affects every variable with a reference to it. The
trace() statement sends tom,jack to the Output panel.
In the following example,
myArray contains an Array object, so you pass the array to function
zeroArray() by reference. The function zeroArray() accepts an Array object as a
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
NOTE
Flash uses a zero-based index, which means that 0 is the first item in the array, 1 is
the second, and so on.

Table of Contents

Related product manuals