Actionscript 3: how to pass an array by reference to a function and update that function?

ActionScript 3, by default, passes an array by reference. I must make the rookie mistake. Here is a snapshot of my code:

private function testFunc():void {
    var testArray:Array=new Array();
    myFunction(testArray);
    trace(testArray); // []; length=0
}

private function myFunction(tArray:Array):void {
    tArray = myOtherFunction();
    trace(tArray); // 0, 1, 2; length=3
}

private function myOtherFunction():Array {
    var x:Array=new Array;
    for (var i:int=0; i<3; i++)
       x[i]=i;
    return x;
}

      

I can see that it is tArray

correct but testArray

always empty. Any idea how to make it testArray

equal tArray

? Thanks in advance.

http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000049.html

UPDATE:

For what it's worth, I found the following change (hack) to work:

private function myFunction(tArray:Array):void {
    var Z:Array=new Array;
    Z = myOtherFunction();
    for (var i:int=0; i<Z.length; i++)
        tArray[i]=Z[i];
}

      

George's solution is the best design.

+3


source to share


3 answers


When you pass testArray as a parameter to myFunction, its reference is copied and assigned to the local reference tArray, so inside myFunction the tArray points to the same object as testArray, but is actually a different reference. This is why the testArray itself does not change when the tArray reference changes.

private function testFunc():void {
    var testArray:Array=new Array(); 
    // testArray is a local variable, 
    // its value is a reference to an Array object
    myFunction(testArray);
    trace(testArray); // []; length=0
}

private function myFunction(tArray:Array):void {
    // tArray is a local variable, which value equals to testArray
    tArray = myOtherFunction(); 
    // now you changed it and tArray no longer points to the old array
    // however testArray inside of testFunc stays the same
    trace(tArray); // 0, 1, 2; length=3
}

      



What you might want:

private function testFunc():void {
    var testArray:Array=new Array();
    testArray = myFunction(testArray);
    trace(testArray); // 0, 1, 2; length=3
}

private function myFunction(tArray:Array):Array {
    // do what you want with tArray
    tArray = myOtherFunction();
    trace(tArray); // 0, 1, 2; length=3
    // return new value of the tArray
    return tArray;
}

private function myOtherFunction():Array {
    var x:Array=new Array;
    for (var i:int=0; i<3; i++)
       x[i]=i;
    return x;
}

      

+4


source


What basically happens is that you've passed your array to a function, which in turn creates a local reference to it (inside the function).

This means that if you decide to go and assign the newly created array to a local variable ( tArray

in your case) it will be used instead and the original will keep the original instance.



This is the same for any object, for example with a sprite:

var sprite:Sprite = new Sprite();

// We set the original Sprite x to 10.
sprite.x = 10;

function mutilate(subject:Sprite):void
{
    // Notice here how we are making a new instance. This won't assign a new
    // instance to the "sprite" property that we've passed here, but rather
    // a new instance to the local variable "subject".
    subject = new Sprite();

    // And here we set the x of the new instance to 20.
    subject.x = 20;
}


mutilate(sprite);
trace(sprite.x); // Outputs 10, though you may have expected 20.

      

+2


source


Take the return of myOtherFunction () iterating through it and then call tArray.push () on each element. Currently, all you are doing is changing the local reference to the newly created object.

You need to add items to the original array, which will also lose reference when assigned.

0


source







All Articles