Deep cloning in ActionScript

What is the best method for deep cloning objects in actionscript?

+7
source share
2 answers

The best way to do this is to use ByteArray with the writeObject method. Like this:

function clone(source:Object):* { var copier:ByteArray = new ByteArray(); copier.writeObject(source); copier.position = 0; return(copier.readObject()); } 

More about this here: http://www.kirupa.com/forum/showpost.php?p=1897368&postcount;=77

+10
source

If you are trying to deeply clone a display object, this is the only way for me:

  public static function clone(target:DisplayObject ):DisplayObject { var bitmapClone:Bitmap = null; var bitmapData:BitmapData = new BitmapData(target.width,target.height,true,0x00000000); bitmapData.draw(target); bitmapClone = new Bitmap(bitmapData); bitmapClone.smoothing = true; return bitmapClone; } 

Please note that this will only visually copy the object. It will not copy methods or properties. I used this when I uploaded external images and used them in several places.

0
source

All Articles