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;
    }
  }
}

No Comments        

Recent Posts

Flexunit Asyncrhonous Setups

If you ever need to load some external data before running a test, or need to wait for a component’s creation complete event, then you know the inadequacies of the Flexunit framework when it comes to asynchronous operations. One approach you might think of is to load the XML data, for example, in the setUp() [...]

FlashDevelop Setter and Getter Snippets

FlashDevelop, the Flash IDE I am using at the moment, has support for saving snippets of code that is general and reusable, like setters and getters. The default setter and getter snippets simply add the skeleton code and require the developer to actually type in variable names, and types. I found this amount of extra [...]

Flex MenuItem Actions

So, gonna write this here because I will forget how to do it. Essentially, this approach eliminates the need to conditionally branch on menu item properties. Before we would do something like:

private function menuHandler(event:MouseEvent):void {
if (event.item.@label == ‘Save’) {
f = File.destopDirectory;
[...]

HP Laptop Linux Boot Parameters

Instead of me wracking my braing trying to remember the exact boot parameters to make my HP laptop not break when I update the kernel, I am posting it here. Hopefully someone else may find it useful as well. My specific laptop is the HP dv9000 series special edition I bought at Best Buy about [...]

World of Goo

Not often do I write personally here outside of programming, but I must make an exception for World of Goo. It’s a game unlike anything else really, maybe a little like the Fantastic Contraption flash game in that they are both logic puzzles. Otherwise it’s a great unique game in which you can lose yourself.
Try [...]

Google Chrome and Firefox 3 Benchmark Comparisons

Google released the beta for their new browser called Google Chrome and one of the features they talked about was the new Javascript virtual machine. So to test their claims of speed I ran Sun’s SunSpider Javascript benchmark on both. The results of the tests follow:
Firefox 3
Google Chrome
As you can see, Google chrome is much [...]

OpenMP Examples

Here are some usage examples for the C++ library available in the GNU C++ compiler versions 4.2 and up, and probably others.
This first example shows how to use the parallel for construct to iterate over a set of neurons. Each thread here gets an equal share of the current layer’s neurons. How big of a [...]

CakePHP Getting Tree Path

In a reversal of yesterday’s post is today’s, getting a path to a node, or nodes. Basically we just use the core getpath() function of the Tree behavior and traverse that path.

function setTreePath(&$data, $path=’tree_path’, $label=’name’) {
if (!is_array($data) || !in_array(’Tree’, $this->actsAs)) {
return $data;
[...]

CakePHP Tree Find By Path

Wrote these little functions to help with finding tree nodes by specifying a path of values. For example if we had a tree of categories with similar names like TVs->Color and Movies->Color, we can’t find by the category name since its not unique, but we can search by pathing like tvs/color or movies/color. So to [...]