如何在 Flex 中克隆对象? [英] How to clone an object in Flex?

查看:32
本文介绍了如何在 Flex 中克隆对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想克隆一个 Canvas 对象,其中包含一个 Degrafa Surface,其中包含多个 Geometry 形状.

I want to clone a Canvas object, which contains a Degrafa Surface with several Geometry shapes.

我尝试了幼稚的方法:

return ObjectUtil.copy(graph_area) as Canvas;

导致错误:

TypeError: Error #1034: Type Coercion failed: cannot convert Object@63b1b51 to com.degrafa.geometry.Geometry.
TypeError: Error #1034: Type Coercion failed: cannot convert Object@63b1039 to com.degrafa.geometry.Geometry.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at mx.core::Container/addChildAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2196]
    at mx.core::Container/addChild()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2140] ...

推荐答案

你要的就是所谓的深拷贝,生成一个新的实例,和原来的信息一样.

What you want is called a deep copy, generate a new instance with the same information of the original.

我知道怎么做的唯一方法是使用 ByteArray,如下所示:

The only way I know how to do it is using ByteArray as follows:

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

AS3 确实缺少 Object.clone()...

AS3 is really lacking Object.clone()...

这篇关于如何在 Flex 中克隆对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆