Copying a Flash MovieClip, or The Flash Holy Grail

Ran into a nice solution for copying arbitrary MovieClips at runtime today. I am designing a spot the difference game akin to 6 Differences with the requirement that, upon finding a difference, the scene should go to the “correct” state depending on which difference was defined as master, among a few other criterion. This required that I somehow duplicate, as much as is possible, the state of the “master” difference, when appropriate.

At any rate, the solution comes from Danny Burbol’s blog, which apparently came from a Experts Exchange thread (damn people charging for information). I modified the solution a bit to fit my needs, but not by a whole lot. Sometimes I only need to copy the instance and I can reconstitute the MovieClip from there but other times I will need to get the whole shebang. It’s pretty handy in general. So my version follows (as a bonus I include my handy-dandy clearAllChildren function):

package com.bmm.utils {
  import flash.display.DisplayObject;
  import flash.display.DisplayObjectContainer;
  import flash.geom.Rectangle;

  /**
   * ...
   * @author BenS
   */
  public class UIUtils {

    public static function clearAllChildren(c:DisplayObjectContainer):void {
      if (c == null || c.numChildren == 0) return;

      var i:int = 0, n:int = c.numChildren;
      for (i = 0; i < n; i++) {
        c.removeChildAt(0);
      }
    }

    public static function simpleDisplayObjectClone(source:DisplayObject):DisplayObject {
      var cc:Class = Object(source).constructor;
      return new cc();
    }

    public static function displayObjectClone(source:DisplayObject):DisplayObject {
      var copy:DisplayObject = UIUtils.simpleDisplayObjectClone(source);

      copy.transform = source.transform;
      copy.filters = source.filters;
      copy.cacheAsBitmap = source.cacheAsBitmap;
      copy.opaqueBackground = source.opaqueBackground;
      if (source.scale9Grid) {
        var r:Rectangle = source.scale9Grid;
        copy.scale9Grid = r;
      }

      return copy;
    }
  }
}

This entry was posted on Friday, May 15th, 2009 at 7:41 pm and is filed under Actionscript, C/C++, Flash, Flex, Front Page. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “Copying a Flash MovieClip, or The Flash Holy Grail”

  1. Gary Benade Says:

    Here’s my version :)

    public static function clearAllChildren(c:DisplayObjectContainer):void {
    while( c.numChildren > 0)
    c.removeChildAt(0);
    }

  2. Gb Says:

    Hi there,

    I am a Actionscript newbie this solution looks great !
    but I am having difficulties getting it working.

    Could you possibly if you have time post an example of it in use.
    or zip example.

    Thanks a lot .

    Gb

  3. Bilaligh Says:

    Uncertain if it is my Machine only because I still had never time to upgrading and am continue utilising Opera 9.5 but My spouse and i viewed some sort of odd letters all around the website . such as Caution: Impossible modify footer information … and so on. It is actually a little bit abnormal simply because for me web-sites like yahoo and google or simply my very own one cheap flights to bahamas perform very good for me.

  4. Signe Aspley Says:

    Sick and tired of getting low amounts of useless visitors for your site? Well i want to inform you of a fresh underground tactic that makes myself $900 on a daily basis on 100% AUTOPILOT. I could truthfully be here all day and going into detail but why dont you simply check their website out? There is really a excellent video that explains everything. So if your seriously interested in producing effortless cash this is the site for you. Auto Traffic Avalanche

Leave a Reply