显示对象(sprite、Movieclip等)clone

public static function clone(target:DisplayObject, autoAdd:Boolean = false):DisplayObject
              {
                      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;
              }

删除容器内所有元素

  public static function dispose(s:Sprite):void{
                        var index:int = s.numChildren;
                        while(index>0){
                                var d:DisplayObject =  s.getChildAt(0) ;
                                var obj:Sprite = d as Sprite;
                                if(obj&&obj.numChildren>0){
                                        dispose(obj);
                                }else if(d&&d.parent){
                                        try{
/*                                                var iData:IDataVO = d as IDataVO;
                                                if(iData){
                                                        iData.dispose();
                                                }*/
                                        }catch(e:Error){
                                                                   
                                        }finally{
                                                d.parent.removeChild(d);
                                        }
                                }
                                index--;
                        }
                }

来自:http://bbs.9ria.com/thread-275821-1-1.html

你可能感兴趣的:(as3,显示对象的复制)