智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...
随笔 - 991, 文章 - 0, 评论 - 27, 阅读 - 341万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

替代复制方法duplicateMovieClip Replacement

Posted on   Bill Yuan  阅读(1293)  评论(0编辑  收藏  举报
一般的,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中获取复制它的信息 。
(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 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的设计模式综述
点击右上角即可分享
微信分享提示