在动作脚本 3 flex 4 中使用 DisplayObject(Group ,UIComponent) 剪切复制粘贴

问题描述:

我正在我的应用程序中实现剪切复制粘贴,例如 cacoo.但我在这些操作中遇到了问题.我正在使用剪切复制粘贴背后的想法

I am implementing Cut Copy Paste in my application like cacoo. but I face problem during these operation. i'm using idea behind cut copy paste

var className:String = getQualifiedClassName(objcut.getItemAt(i))
var klass:Class = getDefinitionByName(className) as Class
var cloneObject:* = new klass()

所以我无法保留对象的所有属性.在 flex 4 中执行这些操作还有其他想法.如何在 Flex 4(as3) 中复制图形对象.复制一个对象并多次粘贴.

so i'm not able to preserve all property of object. There is any other idea to perform these operation in flex 4.how can i copy an Graphical object in Flex 4(as3). Copy an Object and paste multiple times.

复制带有属性的对象的最简单方法是使用 ByteArray:

The simplest way to make a copy of object with properties is using ByteArray:

public static function copy(value:Object):Object
{
    if (!value)
        return null;

    //register object class to prevent Error #1034: Type Coercion failed
    registerClassAlias(getQualifiedClassName(value), value.constructor);

    var buffer:ByteArray = new ByteArray();
    buffer.writeObject(value);
    buffer.position = 0;
    var result:Object = buffer.readObject();
    return result;
}

但是对于嵌套类,您仍然会收到错误 #1034.在进行复制之前,您需要为所有嵌套类注册别名以防止这种情况发生,例如在某些启动方法中.

But you can still get the error #1034 for nested classes. You need register aliases for all nested classes to prevent this before making copy, for example in some startup method.