« Using .ini Settings | ^ Main | Language Overload »


Jack the (_root) Hacker

I did this a while back but revisited it for a Flash module I am creating for a client. So you know how I work…I HATE CODE IN THE .fla!

If I don’t use a root hack I use an include (picked that up from Ron Haberle). I used to initialize a Controller class and that would handle all of my app code. Ron showed me how he uses an include file and I loved that even better. All of my fla’s have 1 line of code now: #include “myFLAname.as”. That just feels right to me.

Enough about includes and my flow…how about some _root hacking?!?

Peep the code below. Basically on the root (or in an include) put the first piece of code. The only thing you have to change is your class name. I conveniently named my Root.as and store it in a class folder (for this demo I called it myclass). Just replace (ctrl+f in Flash 8) myclass.Root with yourclass.ClassName and you’re done.

Now scroll a little lower and you will see the my Root class. I put the _root hack stuff inside of a comment block for clarity. That way I know I don’t have to touch that stuff anymore. From the onLoad method I call init() which is where I start initializing my app.

I owe the original code to JesterXL (aka Jesse Warden). This post is really to expose the process a little more by opening it up to my reader base as well. It is a very sweet concept and it felt GREAT to implement it tonight.

Hope this helps…

//Code on root or in file included on root
//Root Hack; Thx JXL!
//allows _root to be a class
import myclasses.Root;

var depend:Root;
delete depend;

this.__proto__ = _global.myclasses.Root.prototype;
_global.myclasses.Root.apply(this, null);

if (this.calledOnLoad == false) {
    this.onLoad();
}
//end

//Root.as class
class myclasses.Root extends MovieClip{
    /*
       Root Hack...
    */
       public var calledOnLoad:Boolean = false;
       
       public function onLoad():Void {
           if (calledOnLoad == false) {
               calledOnLoad = true;
               //init app
               init();
            }
        }
    /*
       End Root Hack
    */
    
    public function init():Void{
        //start init'ing app here
     }
    
    public function toString(Void):MovieClip{
        return this;
     }
}
//end

Posted by John C. Bland II on January 24, 2006 1:43 AM |

TrackBack

TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/11

Post a comment