Resistence is futile: Assimilating _root in FAME

In my previous post on FAME, I mentioned my preference for remapping the scope of the document class to make it _root. Playing with it a little more, it became apparent that I didn't take it far enough. Merely remapping "this" to the document class' scope is not enough because we are not taking the prototype chain into consideration. Instead, what we need to do is fully assimilate _root, in true Borg-style, using a trick dating back to the days of the Rebel Alliance :)

Here's an updated example:

class Application extends MovieClip
{
  var tf:TextField;

  function Application ( target )
  {
    Flashout.log("Application instance. Initially: " + this);
    Flashout.log ("Assimilating " + target );
    Flashout.log ("We are FAME, you will be assimilated. Resistence is futile...");

    // Assimilate the target
    target.__proto__ = this.__proto__;
    target.__constructor__ = Application;

    this = target;

    Flashout.log ("Assimilation complete. We are " + this );

    // Creates a TextField sized 100x600 at pos 100, 100
    createTextField("tf", 0, 100, 100, 800, 600);

    // Write out who we are
    tf.text = String ( "We are " + this );
  }

  // Example of a private method in the Application class
  private function onEnterFrame ()
  {
    // Move the text field around randomly
    tf._x += Math.random() < .5 ? -1:1 * Math.random();
    tf._y += Math.random() < .5 ? -1:1 * Math.random();
  }

  static function main ()
  {
    // Create an Application instance and
    // have it assimilate _root.
    var test:Application = new Application( _root );
  }
}

If all goes well, you should get a textfield that randomly spasms around your screen and the following in your Flashout Logs:

Application instance. Initially: [object Object] Assimilating _level0 We are FAME, you will be assimilated. Resistence is futile... Assimilation complete. We are _level0

Comments