0

I'll need to clone some objects that have several types of instances. I've searched using a search engine and found some examples however I got error after I tested them.

Example: AS3 - Clone an object

import Line;
import OtherClass;

// Set lines
var obj1:Object={
    environmentLine:new EnvironmentLine(),
    lineX:100,
    lineY:100
};
addChild(obj1.environmentLine);

var line:Line = new Line();
line.clone(obj1);


This gives me error: Line.as, Line 12, Column 3 1170: Function does not return a value.

Example: https://gist.github.com/PrimaryFeather/6914861

import com.gamua.flox.*;
import com.gamua.flox.utils.*;
import com.gamua.flox.events.*;

// Set lines
var obj1:Object={
    environmentLine:new EnvironmentLine(),
    lineX:100,
    lineY:100
};
addChild(obj1.environmentLine);

var clone:Object = new cloneObject(obj1);

This code is giving me warning: TypeError: Error #1064: Cannot call method global/com.gamua.flox.utils::cloneObject() as constructor. at Untitled_07_fla::MainTimeline/frame1()

Example: https://code.google.com/p/as3-commons/source/browse/trunk/as3-commons-lang/src/main/actionscript/org/as3commons/lang/ObjectUtils.as?r=1784

import org.as3commons.lang.ObjectUtils;

var num:int = 0;

// Set lines
var obj1:Object={
    environmentLine:new EnvironmentLine(),
    lineX:100,
    lineY:100
};
var objectUtils:ObjectUtils = new ObjectUtils();
objectUtils.clone(obj1);

This gives me error: org\as3commons\lang\ObjectUtils.as, Line 211, Column 69 1046: Type was not found or was not a compile-time constant: IIterator.

Which one is the best for cloning an object and how can I fix the errors? Or is there a good library?

Community
  • 1
  • 1
nikel
  • 653
  • 4
  • 13
  • 24
  • 1
    There is no built-in functionality to clone nested complex objects (rather than Strings, etc). You need to do it by hand - loop through all properties, check type and instantiate new object with the same props. Your other option is to find some 3rd party solution and pray it will work :) – Andrey Popov Jan 19 '15 at 12:28
  • possible duplicate of [AS3 - Clone an object](http://stackoverflow.com/questions/8461988/as3-clone-an-object) – Andrey Popov Jan 19 '15 at 12:36
  • thanks for the answers. I tried the first solution here: http://stackoverflow.com/questions/8461988/as3-clone-an-object I mentioned it in my question. it's giving me error: *Line.as, Line 12, Column 3 1170: Function does not return a value.* – nikel Jan 19 '15 at 13:07
  • That sounds like an error in your code. Check the return type on the function at Line 12 of the Line class. If the function returns nothing, it should be void. – net.uk.sweet Jan 19 '15 at 13:18
  • I tried this: http://sudrap.org/paste/text/501969/ It's giving me error: *Line.as, Line 12, Column 18 1067: Implicit coercion of a value of type OtherClass to an unrelated type Line.* – nikel Jan 19 '15 at 14:28

1 Answers1

1

Boddy you should search more on Google. This is well discussed topic on many places:

https://gist.github.com/PrimaryFeather/6914861

http://www.tomauger.com/2009/flash-actionscript/actionscript-3-gotchas-copy-by-reference-deep-copy-and-read-only-objects

AS3 - Clone an object

Community
  • 1
  • 1
Andrey Popov
  • 7,362
  • 4
  • 38
  • 58