13 May 2005

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

Add Your Comment

Spam Protection by WP-SpamFree

Resistence is futile: Assimilating _root in FAME

  1. OMG.. plus add this
    static var SymbolName:String = “__Packages.com.flashweek.TestClip”;
    static var SymbolOwner:Object = TestClip;
    static var SymbolLinked = Object.registerClass(SymbolName, SymbolOwner);

    to every file u wanna be able to use from another file – feel myself as a masochist :)

    Alex
  2. I borged my (Singleton-)-Application to _root.
    If I call function myfunc of the Application from another class, its no longer borged:

    class Application:
    function myfunc() {
    trace(this); //output:[object] instead of _root
    }

    otherclass() {
    function onRelease() {
    $app.myfunc();
    }
    }

    Is there some kind of workaround?

    thx,

    rainer

    saneinsane
  3. Rainer, are you sure that it’s not the event handler losing reference due to lack of method closures?

    How are you storing the singleton instance?

    Aral Balkan
  4. I’ve run into that problem too. While that is easy to deal with in the flash IDE it takes a little thought in pure asctionscript. The big thing to realize is that _root has to do with the main swf that loads all other swfs.

    In the flash IDE you would just call:
    var app:Application = new Application ( _this );
    from the timeline and it works. I had a similar problem and fixed it this way. But then I thought about how I would fix the problem in pure code.

    The answer is arguments. No don’t have an argument with the people you are working with. The arguments property of any function will have an argument[0] that is a reference to the calling object. So in pure code, the main function would look like:

    public static function main():Void
    {
    var app:Application = new Application ( arguments[0] );
    }

    I know this is way past the time of these questions but I hope it helps someone else.

    Daryl Ducharme
  5. Hi!
    Can I use this code on the site of a company of solutions in web design and multimedia?
    Thanks!

    Leandro Dias
  6. Of course, go right ahead, it’s open source :)

    Aral