سؤال

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...

هل كانت مفيدة؟

المحلول 2

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

نصائح أخرى

In actionscript 3 you must add a listener, and not use onLoad, So instead of MovieClip.onLoad() do:

MovieClip.addEventListener(Event.ADDED,mcLoaded);

function mcLoaded(e:Event):void {

trace("MovieClip added.");

}

As for the question asked, ADDED / ADDED_TO_STAGE run before ENTER_FRAME.

This solution is for timeline code (Not a separate .as file. OP has the solution for the other case.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top