Search code examples
flashactionscriptflash-cs4

What happens first, MovieClip.onLoad() or Event.ENTER_FRAME?


I'm making a small game in Flash as a school project, and I was wondering what would trigger first when a movie clip is placed-- the onLoad function, or the ENTER_FRAME event.

Any help would be greatly appreciated.

EDIT: I removed onLoad, because it isn't actually called when the object is put on the stage.

var loaded:Boolean = false;
var angle:Number = 0; //in radians
this.addEventListener(Event.ENTER_FRAME, update);
function init():void {
    //get projectile position based on relation to mouse and spawning point.
    trace("init");
    loaded = true;
    this.angle = Math.atan2(mouseY - this.y, mouseX - this.x);
}
function update(e:Event):void {
    /* TRIG TIME! Move the object a certain amount
     * of pixels-- based on the delta, in the specified angle.
     */
    if (!loaded) {
        init();
    }
    trace("update");
    var slope:Number = Math.tan(angle);
}

This method still probably doesn't work, though...


Solution

  • onLoad doesn't seem to work the way I expected it to in this case-- I solved this by changing the onLoad function to an ADDED_TO_STAGE event. I moved all of the actionscript code for the class out of the flash file, and into a separate actionscript file, and put the event listeners for event.ENTER_FRAME and event.ADDED_TO_STAGE events into the class' constructor, and the now entire class works like a charm.

    public function Projectile() {
        this.addEventListener(Event.ADDED_TO_STAGE, init);
        this.addEventListener(Event.ENTER_FRAME, update);
        super();
    }
    

    The function init was called before the function update, and now I can continue with my work!

    Special thanks to Don for helping me solve this dilemma