AS3 Preloader Pause after External file Loaded?

Hey Guys,

Back again with another doozy. I have a preloader.swf that loads another swf (main.swf). It all works fine, however I would like the loading bar to stay on 100 (once it gets there) for 2-3 seconds before the main.swf opens, currently as expected from the code below, as soon as it gets to 100% it flicks straight over to the main.swf.

I am using tweener, and I could achieve this easily using the onComplete code to call this.addChild(loader);, however, the problem is, both my preloader and my main swf are liquid layouts, and if I were to call the this.addChild(loader); at the end in its own function, then it throws a

TypeError: Error #1009: Cannot access a property or method of a null object reference.
. The only way to get around this error is to put the this.addChild(loader); so that it loads upfront. After a bit of google research, i found this error occurs due to the stage properties already been set in the preloader.swf (from liquid layout)then once the main.swf loads it tries to set them again…So is there another way i can cause a delay for 2-3 seconds once my preloader hits 100?

Below is my preloader code with liquid layout settings

//LIQUID LAYOUT
stage.align=StageAlign.TOP_LEFT;
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE, resizeHandlerLoading);
addEventListener(Event.ENTER_FRAME, resizeHandlerLoading);


function resizeHandlerLoading(event:Event):void {
    removeEventListener(Event.ENTER_FRAME, resizeHandlerLoading);
    var sw:Number=MovieClip(root).stage.stageWidth;
    var sh:Number=MovieClip(root).stage.stageHeight;
    trace("StageWidth: " + sw);
    trace("StageHeight: " + sh);
    backgroundMC.x=0;
    backgroundMC.y=0;
    backgroundMC.width=sw;
    backgroundMC.height=sh;
    maskBox.width=wmediaText.width;
    maskBox.x=(sw/2) - (maskBox.width/2);
    maskBox.y=(sh/2) - (maskBox.height/2);
    wmediaText.x=(sw/2) - (wmediaText.width/2);
    wmediaText.y=maskBox.y;
    pLoaded.x=(sw/2) - (pLoaded.width/2);
    pLoaded.y=(sh/2) - (pLoaded.height/2)+60;
}

//PRELOADER

var request:URLRequest=new URLRequest("main.swf");
var loader:Loader = new Loader();

loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

function loadProgress(event:ProgressEvent):void {
    var percentLoaded:Number=event.bytesLoaded/event.bytesTotal;
    percentLoaded=Math.round(percentLoaded*100);
    this.pLoaded.text=String(uint(percentLoaded))+"% LOADED";
    maskBox.scaleX=percentLoaded/100;
}
function loadComplete(event:Event):void {
    trace("Load Complete");
    this.maskBox.visible=false;
    this.pLoaded.visible=false;
    stage.removeEventListener(Event.RESIZE, resizeHandlerLoading);
    loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProgress);
    loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
    
}
this.addChild(loader);
loader.load(request);

Any help would be greatly appreciated…either

  1. using my tweener onComplete method and fixing the error caused by preexisting stage properties

  2. Thinking of an alternate way to delay the main.swf from opening straight away (keeping in mind that *this.addChild(loader); *would need to stay where it is…

PS - here is a link to a person who was getting the #1009 error with preloader and liquid layout, their solution works, but won’t allow me to use a tweener delay…SitePoint Forums | Web Development & Design Community