all groups > flash actionscript > december 2004 >
You're in the

flash actionscript

group:

Duplicating an object is it possible ?


Re: Duplicating an object is it possible ? David Stiller
12/27/2004 4:35:59 PM
flash actionscript:
juankpro,

Something worried me about your example, so I gave it a quick try.
Unfortunately, you'll see that the handle "objB" still refers to the same
object as "objA" via your prototype. The culprit is the line ...

newObj = this;

.... which should read ...

newObj[prop] = this[prop];

.... where prop is the property in question.

A corrected rundown follows.

//Original object
_root.objA = new Object();
objA.fruit = "apples";
objA.veggies = "corn";
objA.meat = "beef";

// Prototype clone
Object.prototype.clone = function(){
var newObj = new Object();
for(var prop in this){
newObj[prop] = this[prop];
}
return newObj;
};

// Clone object
objB = objA.clone();

// Change object properties
objB.fruit = "oranges";
objB.veggies = "spinach";
objB.meat = "pork";

trace("A fruit: " + objA.fruit);
trace("A veggies: " + objA.veggies);
trace("A meat: " + objA.meat);

trace("B fruit: " + objB.fruit);
trace("B veggies: " + objB.veggies);
trace("B meat: " + objB.meat);



David
stiller (at) quip (dot) net
"Luck is the residue of good design."

Re: Duplicating an object is it possible ? David Stiller
12/27/2004 4:50:30 PM
juankpro,

No problem. :) It's frustrating when that kind of thing happens. The
web forum sees things like bracket-i-bracket as an italics formatting
mechanism, rather than, for example, "array sub i". That's why I changed
your variable i to prop.

It all comes out in the wash as long as the OP gets it. :)


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

Duplicating an object is it possible ? 333444
12/27/2004 8:32:17 PM
I have and initial object and I wish to have an independant duplicate of it.
No matter what I try both objects are updated when I change the duplicata.

Ex :

_root.menu = new Object();
_root.menu.option = "BLUE";

_root.newMenu = _root.menu;
_root.newMenu.option = "BROWN";

trace( _root.menu.option ); // returns BROWN, but I never set this to
BROWN..... it's suppose to be BLUE.


Re: Duplicating an object is it possible ? juankpro
12/27/2004 9:12:08 PM
Sadly objects in flash and in many other languages are copied by reference that
means that when you say something like root.newMenu = _root.menu; you are not
copying menu to newMenu, you are just referencing the same object but with
different name. To copy you can use this code at the beginning of your movie.

Object.prototype.clone = function(){
var newObj = new Object();
for(var i in this){
newObj = this;
}
return newObj;
};

Then to copy an object do it like this:

_root.menu = _root.menu.clone();
Re: Duplicating an object is it possible ? juankpro
12/27/2004 9:41:18 PM
Re: Duplicating an object is it possible ? juankpro
12/27/2004 9:43:21 PM
I know what happened I edited the message but when editing it al the words
surrounded by [ ] dissapeared becuase Edit button is not working correctly from
my computer. It erases the newline characters and replaces them with literal
<BR> tags.
Re: Duplicating an object is it possible ? juankpro
12/27/2004 9:43:55 PM
AddThis Social Bookmark Button