Posted By

damarev on 03/03/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

frdmsun
iamgowan


duplicateMovieClip Replacement


 / Published in: ActionScript 3
 

var newInstance:Sprite = duplicateDisplayObject(myOldSprite, true); newInstance.x += 100; // shift to see duplicate

  1. package com.senocular.display {
  2.  
  3. import flash.display.DisplayObject;
  4. import flash.geom.Rectangle;
  5.  
  6. /**
  7.   * duplicateDisplayObject
  8.   * creates a duplicate of the DisplayObject passed.
  9.   * similar to duplicateMovieClip in AVM1
  10.   * @param target the display object to duplicate
  11.   * @param autoAdd if true, adds the duplicate to the display list
  12.   * in which target was located
  13.   * @return a duplicate instance of target
  14.   */
  15. public function duplicateDisplayObject(target:DisplayObject, autoAdd:Boolean = false):DisplayObject {
  16. // create duplicate
  17. var targetClass:Class = Object(target).constructor;
  18. var duplicate:DisplayObject = new targetClass();
  19.  
  20. // duplicate properties
  21. duplicate.transform = target.transform;
  22. duplicate.filters = target.filters;
  23. duplicate.cacheAsBitmap = target.cacheAsBitmap;
  24. duplicate.opaqueBackground = target.opaqueBackground;
  25. if (target.scale9Grid) {
  26. var rect:Rectangle = target.scale9Grid;
  27. // WAS Flash 9 bug where returned scale9Grid is 20x larger than assigned
  28. // rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
  29. duplicate.scale9Grid = rect;
  30. }
  31.  
  32. // add to target parent's display list
  33. // if autoAdd was provided as true
  34. if (autoAdd && target.parent) {
  35. target.parent.addChild(duplicate);
  36. }
  37. return duplicate;
  38. }
  39. }

Report this snippet  

You need to login to post a comment.