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
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 :)
by Alex on 2005-05-14 13:17:04
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
by saneinsane on 2005-08-12 16:46:43
How are you storing the singleton instance?
by Aral Balkan on 2005-08-12 17:01:42
by Daryl Ducharme on 2006-07-12 19:30:52
by Leandro Dias on 2009-05-20 23:32:31
by Aral on 2009-05-21 10:57:15