2009-02-19 55 views
8

Tôi muốn sao chép một đối tượng Canvas, trong đó có một bề mặt Degrafa với một số hình dạng hình học.Làm thế nào để sao chép một đối tượng trong Flex?

Tôi đã thử các cách tiếp cận ngây thơ:

return ObjectUtil.copy(graph_area) as Canvas; 

đó dẫn đến lỗi:

TypeError: Error #1034: Type Coercion failed: cannot convert [email protected] to com.degrafa.geometry.Geometry. 
TypeError: Error #1034: Type Coercion failed: cannot convert [email protected] 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] ... 

Trả lời

9

Những gì bạn muốn được gọi là một bản sao sâu, tạo ra một đối tượng mới với thông tin tương tự của bản gốc.

Cách duy nhất tôi biết làm thế nào để làm điều đó là sử dụng ByteArray như sau:

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

AS3 thực sự là thiếu Object.clone() ...

+2

nếu bạn nhìn vào nguồn của ObjectUtil.copy(), nó thực hiện chính xác điều tương tự. –

+0

Vâng, nó đang sử dụng AMF để tuần tự hóa và de-serialize đối tượng. – LiraNuna

+0

tôi gặp lỗi trên mã này ... Lỗi: Lỗi # 2030: Đã xảy ra kết thúc tệp. \t tại flash.utils :: ByteArray/readObject() – Devendra

0

Tôi không nghĩ ObjectUtil.copy sẽ làm việc để nhân bản một khung hình. Theo tài liệu flex:

Sao chép Phương pháp này được thiết kế để sao chép đối tượng dữ liệu, chẳng hạn như thành phần của bộ sưu tập. Nó không nhằm mục đích sao chép một đối tượng UIComponent, chẳng hạn như điều khiển TextInput. Nếu bạn muốn tạo các bản sao của các đối tượng UIComponent cụ thể, bạn có thể tạo một lớp con của thành phần và thực hiện một phương thức clone() hoặc phương thức khác để thực hiện sao chép.

1

tôi thấy mình cố gắng một cái gì đó như thế này than ôi nó vẫn có vẻ không sao chép một TextArea (aka UI Object) ...

public function duplicateObject(sourceObject:*, targetObject:*):void { 
    var buffer:ByteArray = new ByteArray(); 
    buffer.writeObject(sourceObject); 
    buffer.position = 0; 
    targetObject = buffer.readObject(); 
} 
+0

nó sẽ làm việc cho UIComponent hoặc displayObject? – Devendra

1

tôi đã nhận cùng một vấn đề (đối với một giao diện NamedEntity i tạo), tìm câu trả lời ở đây, nhưng chỉ có nó làm việc thực hiện cuộc gọi đến phương thức registerClassAlias ​​(mà tôi lấy từ http://richapps.de/?p=34). Chỉ cần như thế:

public static function clone(namedEntity:NamedEntity):NamedEntity { 
registerClassAlias('test',ReflectionUtil.classByObject(namedEntity)); 
var returnObject:NamedEntity = ObjectUtil.copy(namedEntity) as NamedEntity; 
} 
7

ObjectUtil

Phương pháp tĩnh ObjectUtil.copy() là AS3 của "Object.clone()":

public static function copy(value:Object):Object 

Copies the specified Object and returns a reference to the copy. The copy is made using a native serialization technique. This means that custom serialization will be respected during the copy.

This method is designed for copying data objects, such as elements of a collection. It is not intended for copying a UIComponent object, such as a TextInput control. If you want to create copies of specific UIComponent objects, you can create a subclass of the component and implement a clone() method, or other method to perform the copy.

+0

làm cách nào để sao chép uiobject? – Devendra

Các vấn đề liên quan