ActionScript Object Duplication

September 4, 2006

Yesterday I posted how to properly copy an ActionScript Array.

Here is an recursive implementation that allows to deep-copy objects (other then MovieClips);

/// duplicate any given Object (not MCs)

Object.prototype.copy = function()
{

var _t = new this.__proto__.constructor(this) ;
for(var i in this)
{

if(typeof this[i] == “object”)

_t[i] = this[i].copy()

else

_t[i] = this[i];

}

return _t;

};

ASSetPropFlags(Object.prototype,[“copy”],1);

I found this code here. Thanks klitze7-c004, I will use this method from now on to copy Arrays , this way I’m sure that the contents get copied as well.

Of course this is how it’s used.

var x:Array = new Array(“1″,”2″,”3”,{a:1,b:2});
var y:Array = x.copy();

Advertisements

8 Responses to “ActionScript Object Duplication”

  1. Sean Says:

    Juuuust what I needed.

    I am constantly amazed that functions like this don’t exist already in actionscript. Flash and I have a love/hate relationship.


  2. Hi Sean,

    I’m glad I could be of help. I have that same love/hate relationship with Flash since the new Flash 9 with ActionScript 3.0, before that it was just a hate relationship. 🙂
    The new AS 3 is ways better then AS 2 but there’s still no deepcopy functionality there!
    Here’s how you do deepcopying in AS 3;
    http://code.ericfeminella.com/classes/as3/DeepCopy.as.html

  3. kristoffe Says:

    here’s one I wrote that is a more versatile copying style than that awesome pure proto you got from senocular. Who is God.

    —–
    //05-13-2008
    //deepClone with an object as a new object for versatility by kristoffe brodeur
    //
    //by using the sNew as the new object tree branch and the same for the old object sTgt, a recursive tree can be built
    //*note: this WILL crash after 256 recursive calls to DeepClone.prototype.copier, so keep your objects relatively simple
    //
    var t=new Array();
    t[0]=”kristoffe”;
    t[1]=”evelynn”;
    t[2]=t[0];
    t[2]=”popeye”;
    trace(t);
    //
    function subNode()
    {
    this.age=100;
    }
    //
    function Node(_sName,_sData)
    {
    this._name=_sName;
    this._data=_sData
    this._deep=new subNode();
    trace(“*”+this._deep);
    }
    var g=new Object();
    g.people=new Array();
    g.people[0]=new Node(“people”,”john”);
    g.people[1]=new Node(“people”,”jill”);
    //—–
    DeepClone.prototype.copier=function(sNew,sTgt)
    {
    //
    for(var b in sTgt)
    {
    trace(“[“+b+”]”);
    if(typeof sTgt[b]==”object”)
    {
    trace(“o->”);
    sNew[b]=new Object();
    this.copier(sNew[b],sTgt[b]);
    }
    else
    {
    sNew[b]=sTgt[b];
    trace(“->”+b);
    }
    }
    }
    //—–
    function DeepClone(sentObj)
    {
    trace(“DeepClone”);
    this._clone=new Object();
    this.copier(this._clone,sentObj);
    //
    }
    tmp=new DeepClone(g.people[0]);
    g.people[2]=tmp._clone;
    g.people[2]._data=”kristoffe”;
    len=g.people.length;
    //
    for(var t in g.people[0])
    {
    trace(t);
    trace(typeof g.people[0][t]);
    }
    //
    for(a1=0;a1<len;a1++)
    {
    trace(“node: “+g.people[a1]._data);
    trace(“nested: “+g.people[a1]._deep.age);
    }


  4. Thanks kristoffe. I’ll leave it to someone else to answer the philosophical question of who god is. 🙂

  5. oliver Says:

    you could create a function that returns a new object? I know duplicate was the way to do it in AS2 but you can write the function to return an object, and just reuse that, no?

    I recently used this for papervision, to re-create the same object multiple times dynamically:

    function addClassicWall():DisplayObject3D {
    var classic_wall = new DisplayObject3D();
    //scene.addChild(pivot);
    var wall_classic:Plane = new Plane(bfm_wall_classic, 200, 200, 2, 6);
    var floor_classic:Plane = new Plane(bfm_floor_classic, 200, 800, 4, 8);
    var roof_classic:Plane = new Plane(bfm_roof_classic, 200, 800, 2, 4);
    classic_wall.addChild(wall_classic);
    classic_wall.addChild(floor_classic);
    classic_wall.addChild(roof_classic);
    floor_classic.rotationX = 90;
    floor_classic.y -= 100;
    floor_classic.z -= 400;
    roof_classic.rotationX -= 90;
    roof_classic.y += 100;
    roof_classic.z -= 400;
    return classic_wall;
    }

    // testing duplication of “cubes”
    var wall_test = new DisplayObject3D();
    wall_test = addClassicWall();
    scene.addChild(wall_test);

    var wall_test2 = new DisplayObject3D();
    wall_test2 = addClassicWall();
    scene.addChild(wall_test2);
    wall_test2.x -= 201;

    var wall_test3 = new DisplayObject3D();
    wall_test3 = addClassicWall();
    scene.addChild(wall_test3);
    wall_test3.x += 201;

  6. Abdessamad Says:

    thanx theolagendijk
    i got an error:
    1180: Call to a possibly undefined method ASSetPropFlags.

  7. Alexwebmaster Says:

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru


  8. Feel free to give me an incoming link. 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: