Avoiding Error #1009 when loading external swf

Hello,

I’ve often encountered this error when loading one swf into another, especially when loading papervision pv3d examples, and finally figured out why: when the loaded swf refers to stage anywhere in its startup sequence stage does not exist until that swf has been added to the stage…

The solution was to move startup code to a function and test for availability of stage…


public function Main() {
    if (stage == null) {
        addEventListener(Event.ADDED_TO_STAGE, startup);
    } else {
        startup();
    }
}

private function startup(event : Event = null) : void {
    stage.scaleMode = StageScaleMode.NO_SCALE;
    init3D();
    createCube();
    stage.quality = StageQuality.LOW;
    this.addEventListener(Event.ENTER_FRAME, loop);
}

Hope this helps someone.