一般的,as3中,复制的概念经变成addChild了,如果真要复制舞台上的mc,可以参考一下下面两种简单的方法:
如果myMC时间轴上有代码,不用linkage都可以实现复制了~~
构造器方法:
但是如果在舞台上的mc经过缩放或者旋转了,上面的代码就实现不了类似as1/as2中的复制了,下面看一下老外写的一个duplicateDisplayObject类,可以比较好的解决(文章做了翻译):
转自:http://frankfenghua.blogspot.com/2007/08/as3-duplicatemovieclip-replacement.html
-------以下为原文------------------------------------------------------------------------------
ActionScript 3 no longer has a duplicateMovieClip method for MovieClip instances (or any DisplayObject instances). Instead, it's suggested that you just create a new instance of the display object you wish to duplicate using its constructor. This, however, is not the same as duplicateMovieClip, and, really, is more like using AS1 and AS2's attachMovieClip. For a more accurate representation of duplicateMovieClip in AS3, consider the following function:
ActionScript 3不再有duplicateMovieClip方法的影片剪辑实例(或任何DisplayObject实例)。替代的,它提示您只需建立一个新的实例显示对象要重复使用其构造。然而这是不同概念的复制,其实更像是用AS1或AS2的 attachMovieClip 。在AS3中如果想要更为准确duplicateMovieClip复制,考虑下面的函数:
代码
usage:
用法:
代码
The only thing duplicateMovieClip does that this does not is copy dynamic drawing information. Currently, the graphics object in display objects cannot be duplicated so there is no way to obtain that information for duplicates in duplicateDisplayObject.
这里duplicateMovieClip唯一不能办到的是不能复制动态绘制出的图形。现在,图形对象在显示对象中不能被复制,所以没有办法在duplicateDisplayObject中获取复制它的信息 。
如果myMC时间轴上有代码,不用linkage都可以实现复制了~~
var ClassReference:Class = getDefinitionByName(getQualifiedClassName(myMC)) as Class;
var tmpMC=new ClassReference();
addChild(tmpMC);
var tmpMC=new ClassReference();
addChild(tmpMC);
构造器方法:
var NewClass:Class = myMC.constructor;
var tmpMC:MovieClip = new NewClass();
addChild(tmpMC);
var tmpMC:MovieClip = new NewClass();
addChild(tmpMC);
但是如果在舞台上的mc经过缩放或者旋转了,上面的代码就实现不了类似as1/as2中的复制了,下面看一下老外写的一个duplicateDisplayObject类,可以比较好的解决(文章做了翻译):
转自:http://frankfenghua.blogspot.com/2007/08/as3-duplicatemovieclip-replacement.html
-------以下为原文------------------------------------------------------------------------------
ActionScript 3 no longer has a duplicateMovieClip method for MovieClip instances (or any DisplayObject instances). Instead, it's suggested that you just create a new instance of the display object you wish to duplicate using its constructor. This, however, is not the same as duplicateMovieClip, and, really, is more like using AS1 and AS2's attachMovieClip. For a more accurate representation of duplicateMovieClip in AS3, consider the following function:
ActionScript 3不再有duplicateMovieClip方法的影片剪辑实例(或任何DisplayObject实例)。替代的,它提示您只需建立一个新的实例显示对象要重复使用其构造。然而这是不同概念的复制,其实更像是用AS1或AS2的 attachMovieClip 。在AS3中如果想要更为准确duplicateMovieClip复制,考虑下面的函数:

package com.senocular.display
{
import flash.display.DisplayObject;
import flash.geom.Rectangle;
public function duplicateDisplayObject(target:DisplayObject, autoAdd:Boolean = false):DisplayObject
{
// create duplicate
var targetClass:Class = Object(target).constructor;
var duplicate:DisplayObject = new targetClass();
// duplicate properties
duplicate.transform = target.transform;
duplicate.filters = target.filters;
duplicate.cacheAsBitmap = target.cacheAsBitmap;
duplicate.opaqueBackground = target.opaqueBackground;
if (target.scale9Grid)
{
var rect:Rectangle = target.scale9Grid;
// Flash 9 bug where returned scale9Grid is 20x larger than assigned
rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
duplicate.scale9Grid = rect;
}
// add to target parent's display list
// if autoAdd was provided as true
if (autoAdd && target.parent)
{
target.parent.addChild(duplicate);
}
return duplicate;
}
}
{
import flash.display.DisplayObject;
import flash.geom.Rectangle;
public function duplicateDisplayObject(target:DisplayObject, autoAdd:Boolean = false):DisplayObject
{
// create duplicate
var targetClass:Class = Object(target).constructor;
var duplicate:DisplayObject = new targetClass();
// duplicate properties
duplicate.transform = target.transform;
duplicate.filters = target.filters;
duplicate.cacheAsBitmap = target.cacheAsBitmap;
duplicate.opaqueBackground = target.opaqueBackground;
if (target.scale9Grid)
{
var rect:Rectangle = target.scale9Grid;
// Flash 9 bug where returned scale9Grid is 20x larger than assigned
rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
duplicate.scale9Grid = rect;
}
// add to target parent's display list
// if autoAdd was provided as true
if (autoAdd && target.parent)
{
target.parent.addChild(duplicate);
}
return duplicate;
}
}
usage:
用法:

import com.senocular.display.duplicateDisplayObject;
// create duplicate and assign to newInstance variable
//创建复制并分配给一个实例变量
// using true for autoAdd automatically adds the newInstance
//使用autoAdd中的true值自动添加新实例
// into the display list where myOldSprite is located
//定位myOldSprite并插入显示列表(myOldSprite添加到场景中)
var newInstance:Sprite = duplicateDisplayObject(myOldSprite, true);
newInstance.x += 100; // shift to see duplicate
// create duplicate and assign to newInstance variable
//创建复制并分配给一个实例变量
// using true for autoAdd automatically adds the newInstance
//使用autoAdd中的true值自动添加新实例
// into the display list where myOldSprite is located
//定位myOldSprite并插入显示列表(myOldSprite添加到场景中)
var newInstance:Sprite = duplicateDisplayObject(myOldSprite, true);
newInstance.x += 100; // shift to see duplicate
The only thing duplicateMovieClip does that this does not is copy dynamic drawing information. Currently, the graphics object in display objects cannot be duplicated so there is no way to obtain that information for duplicates in duplicateDisplayObject.
这里duplicateMovieClip唯一不能办到的是不能复制动态绘制出的图形。现在,图形对象在显示对象中不能被复制,所以没有办法在duplicateDisplayObject中获取复制它的信息 。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述