AS3 Chronicles #3: Enter Frame Event & _root Class

You can finally add event listeners to MovieClip events. Whats more, you can register for the enterFrame event and you don’t have to have something “visual” to do it. You just create a new MovieClip or Sprite in your class, register for the event, and never add it to the Display List. In the example below, I just create a MovieClip, register for it’s enterFrame event, and pass in the function to call.

package
{
        import flash.util.trace;
        import flash.display.MovieClip;
        import flash.events.EventType;
        import flash.events.Event;
        
        public class EnterFrameTest extends MovieClip
        {
                // define our movieclip
                private var nonVisual_mc:MovieClip;
                
                // this main function gets executed when our SWF is run
                public function EnterFrameTest()
                {
                        nonVisual_mc = new MovieClip();
                        // register for the enter frame event; no Delegate, yay!
                        nonVisual_mc.addEventListener(EventType.ENTER_FRAME, onEnterFrame);
                }
                
                // event handler function, runs every enter frame
                private function onEnterFrame(event:Event):Void
                {
                        trace("enter frame");
                }
                
        }
}

That’s it!

You’ll notice there is a constructor function that is public. Like all constructors of classes, they are run when a class is created. The cool thing about this class is that it is a “main” class. Main classes are the top level class of ActionScript projects. When you compile an ActionScript Project, as opposed to a Flex Project, it uses that class as the main class, or “_root” as it were, in this case “root”, since underscores have been removed from ActionScript to clean up the API, namely in the MovieClip arena.

So, if you trace out root in your main function:

trace("root: " + root);

She’ll show “root: [object EnterFrameTest]” in the Console Window since this is your main class, and thus the true root of the SWF. To get the effect in AS2, you’d merely override _root’s __proto__ property on frame 1 of your Flash SWF, like so:

import com.jxl.controller.Application;
// do this to ensure class is exported
var depend = com.jxl.controller.Application;
delete depend;

this.__proto__ = _global.com.jxl.controller.Application.prototype;

_global.com.jxl.controller.Application.apply(this, null);// call constructor

if(this.loaded == false)
{
        this.onLoad(); // have to call onLoad manually
}

Flex does something similiar with it’s Application class.

The cool thing here is we finally have a class associated for the _root of our SWF’s, and the base class only has to be Sprite. You could use MovieClip if you wanted (I do in the example above, but always use Sprite when I can). You cannot use Bitmap, since although Bitmap does extend the DisplayObject, he is not a sub-class of Sprite. What this means is, you could create your own “MainClass”, that extends Sprite, and make that your main application class. Cool stuff!