替代复制方法duplicateMovieClip Replacement

一般的,as3中,复制的概念经变成addChild了,如果真要复制舞台上的mc,可以参考一下下面两种简单的方法:

如果myMC时间轴上有代码,不用linkage都可以实现复制了~~

var ClassReference:Class = getDefinitionByName(getQualifiedClassName(myMC)) as Class;
var tmpMC
=new ClassReference();
addChild(tmpMC);

构造器方法:
var NewClass:Class = myMC.constructor; 
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;
    }
}

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


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中获取复制它的信息 。
原文地址:https://www.cnblogs.com/sevenyuan/p/1615499.html