0

我需要克隆一些具有多种类型实例的对象。我使用搜索引擎搜索并找到了一些示例,但是在测试它们后出现错误。

示例:AS3 - 克隆对象

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);


这给了我错误: Line.as,第 12 行,第 3 列 1170:函数不返回值。

示例: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);

这段代码给了我警告:TypeError: Error #1064: Cannot call method global/com.gamua.flox.utils::cloneObject() as constructor。在 Untitled_07_fla::MainTimeline/frame1()

示例: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);

这给了我错误:org\as3commons\lang\ObjectUtils.as, Line 211, Column 69 1046: Type was not found or is not a compile-time constant: IIterator.

哪一个最适合克隆对象,如何修复错误?或者有没有好的图书馆?

4

1 回答 1

1

身体,你应该在谷歌上搜索更多。这在很多地方都得到了很好的讨论:

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 - 克隆一个对象

于 2015-01-19T12:35:24.980 回答