Archive for the ‘Front Page’ Category

Copying a Flash MovieClip, or The Flash Holy Grail

Friday, May 15th, 2009

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

Python Lies

Sunday, February 17th, 2008

This is probably a documented issue, but I think it deserves another mention. Python lies, its the truth. Well, as much as an interpreter can lie.

Say we want to do some math in Python. Say we want to raise large numbers to large numbers. Say those numbers are 324 and 324. Then we would do something like:

<<< 324**324

and see all the pretty numbers spew out from the interpreter. How do we know if this is right or wrong though? Say we use long doubles in C++ to determine the results of this same operation. Then we would do something like:

long double k = 324;
for (int i=0; i<324; ++i) {
    k *= k
}
std::cout << "324^324 = " << k << "n";

But alas, the output is “inf”, so we check the largest long double our compiler and machine can represent and find that it is roughly equal to 1.2e+4932. We then check in python if this number is greater than 324^324 by doing something like:

>>>> (12**4932) > (324**324)
True

However, this contradicts the fact that C++ could not represent 324^324, so Python must be lying. Indeed we check the largest power of 324 we can represent in C++ long doubles, and it turns out to be 11. Thus Python lies.

What does this mean. Don’t rely on Python for numeric accuracy without checking its results.

Update: This post is probably wrong, but I don’t like deleting things. So refer to Tyler’s comment below.