AS3克隆MovieClip

时间:2010-10-22 16:47:48

标签: actionscript-3

以下是我尝试克隆MovieClip的代码,但它不起作用。 如果代码工作正常,我们应该看到两个圆圈。

/*The original MovieClip*/
var circle:MovieClip = new MovieClip();
circle.graphics.beginFill(0xAA0022);
circle.graphics.drawCircle(40, 40, 40);
circle.x=10
addChild(circle);

/*CLONE the MovieClip - IT DOES'T WORK FROM HERE DOWN*/
var cloneCirle:MovieClip = new MovieClip();
    cloneCirle=circle
    cloneCirle.x=60 
    addChild(cloneCirle);

3 个答案:

答案 0 :(得分:8)

执行cloneCircle=circle时,不会复制或克隆任何内容。它只是说变量cloneCircle是原始circle MovieClip的另一个名称。您需要做的是使用Graphics.copyFrom()方法。

试一试:

var cloneCircle:MovieClip = new MovieClip();
cloneCircle.graphics.copyFrom(circle.graphics);
cloneCircle.x = 60;
addChild(cloneCircle);

答案 1 :(得分:3)

这是为了在编译时创建FLA库中存在的舞台对象的副本

对象必须在其“属性”面板中勾选“导出动作脚本”,并在“类”框中输入有效的类名称

如果符号只有一个框架,则只需添加另一个框架,使其注册为MovieClip()而不是Sprite()

private function cloneObject(source:DisplayObject):void
{
    var objectClass:Class = Object(source).constructor;
    var instance:MovieClip = new objectClass() as MovieClip;
    instance.transform = source.transform;
    instance.filters = source.filters;
    instance.cacheAsBitmap = source.cacheAsBitmap;
    instance.opaqueBackground = source.opaqueBackground;
    source.parent.addChild(instance);
    instance.x += 20; // just to show the duplicate exists!
}

http://snipplr.com/view/44734/

答案 2 :(得分:3)

改编自here

function copyClip( clip:MovieClip )
{
    var sourceClass:Class = Object(clip).constructor;
    var duplicate:MovieClip = new sourceClass();
    return duplicate;
}