Skip to main content
Participating Frequently
June 10, 2010
Question

ByRef versus ByValue

  • June 10, 2010
  • 1 reply
  • 1017 views

This wasn't a problem till it was affecting ME!

I want to either copy an Array of MovieClips (Deep Copy) so that the 2 Arrays don't reference each other, or make a copy of an individual Array Element into a seperate object (once again a MovieClip) without it referencing the source Array.

for instance:

var myDef:Class;
var myMC:MovieClip;

var myMC2:MovieClip;

var myArray = new Array();

myDef = Class(getDefinitionByName("mc");
myMC = new myDef();
myMC.x=10;
myMC.y=10;

myMC.name = "Test";

myArray[0]=myMC;

stage.addChild(myArray[0]);

myMC2=myArray[0];

myMC2.x=100;

myMC2.y=100;

The MovieClip that's on the stage (myArray[0]), can be controlled by myMC2, but I want it to be a copy so they don't control one another.

How do I break the Reference and copy byValue instead?

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
June 11, 2010

use the slice() method of the array class to make a copy.


Participating Frequently
June 11, 2010

Still doesn't work.  The slice methos SAYS it will create a copy of the

array, but it doesn't!

I modified the code to use a second array that was filled in by

the slice() method.  But modifying the second array still affects

the first one.  Slice seems to make a "Shallow Copy" or in other

words just makes a copy of the references.

Here's the code U used, you just need a MovieClip in the library

called "mc" in the linkage.

var myDef:Class;
var myMC:MovieClip;
var myArray = new Array();
var myArray2 = new Array();

myDef = Class(getDefinitionByName("mc"));
myMC = new myDef();
myMC.x=50;
myMC.y=50;
myMC.name = "Test";
myArray[0]=myMC;
stage.addChild(myArray[0]);
myArray2=myArray.slice(0,1);
myArray2[0].x=300;

kglad
Community Expert
Community Expert
June 11, 2010

that's no test of the slice() method.  use:

var myDef:Class;
var  myMC:MovieClip;
var myArray = new Array();
var myArray2 = new  Array();

myDef =  Class(getDefinitionByName("mc"));
myMC = new myDef();
myMC.x=50;
myMC.y=50;
myMC.name  = "Test";
myArray[0]=myMC;
stage.addChild(myArray[0]);
myArray2=myArray.slice(0,1);
myArray2[0]="HI";

trace(myArray[0]);  //  should be your class