views:

5376

answers:

7

Hello guys,

I have a Canvas i would like to clone. This canvas hold a Degrafa Surface with several Geometry shapes.

I tried the naive approach of

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] ...

How would you do such a thing ?

Thanks,

+3  A: 

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

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 is really lacking Object.clone()...

LiraNuna
if you look at the source of ObjectUtil.copy(), it does the exact same thing.
jeremy.mooer
Yeah, it's using AMF to serialize and de-serialize the object.
LiraNuna
A: 

I'm having the same problem, and the code posted by LiraLuna is identical to what ObjectUtil.copy actually does. There's something wonky with clonging degrafa objects ...

+2  A: 

Lira, the code you posted is exactly the code from ObjectUtil.copy() method, which is a native method from flex.

A: 

I don't think ObjectUtil.copy will work for cloning a canvas. According to the flex doc :

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.

+1  A: 

I found myself trying something more like this alas it still doesn't seem to copy a TextArea (aka UI Object)...

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

Hi guys, i got the same problem (for a NamedEntity interface i created), looked for the answer here, but only got it working making a call to the registerClassAlias method (which i took from http://richapps.de/?p=34). Just like that:

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

Luiz Henrique Martins Lins Rol
+2  A: 

ObjectUtil

The static method ObjectUtil.copy() is AS3's "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.

Fernando