Flash duplication of an Object - Cloning library?

Deadly 提交于 2020-01-13 11:14:52

问题


This is probably a very simple question, I just don't have the foggiest how to go about it.

I have an Object that I want to duplicate, and don't know how to go about it. Here's my attempt:

var myObj = new ObjectClass();
var duplicate = myObj;
duplicate = null;
myObj.function(); // Error: Null reference

The ObjectClass is very large, inherets and creates children of it's own, and I'm sure there's probably a few singleton classes in there.

Is there a way to duplicate something easily?

Edit: Looks like I'm looking for "Cloning", for which there is no AS3 function, and you apparantly can't clone Private data anyway. Anyone know of a library or a workaround for cloneing a bunch of private data?


回答1:


I got this util function from some blog, can't remember from where so I can't give the credit. It wouldn't work with bitmapdata though. Anyway here it is:

public static function copy(o:Object):Object 
{
    var bytes:ByteArray = new ByteArray( );
    bytes.writeObject( o );
    bytes.position = 0;
    return bytes.readObject( );
}

Usage:
registerClassAlias("com.tests.TestClass", TestClass); var testCopy:TestClass = TestClass(ObjectUtil.copy(test));




回答2:


You can use ObjectUtil.copy() (a Flex library). But you are right that it doesn't support private data. So this is a shot in the dark but I wonder if you serialize it to AMF using ByteArray.writeObject() if that will copy the private data? Might be worth trying.



来源:https://stackoverflow.com/questions/832732/flash-duplication-of-an-object-cloning-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!