actionscript 3 preloader freezes at 100% (SOLVED!)

Have you ever embedded an swf into an html docuement and all seemed fine until you started getting calls from customers saying that the preloader is stuck at 100%?

Well I have, and it took me quite some time to figure this out.

If you embed  different wmode, such as wmode=transparent,  Firefox will load the first frame of your swf but then it appears to lock up!

It turns out that the  loaderInfo Event.COMPLETE and PROGRESS are not firing.

Here is my working preloader:

stop();

var preLoaderPercent:Number = 0;

addEventListener(Event.ENTER_FRAME, loadComplete);

function loadComplete(e:Event):void {

if (this.loaderInfo.bytesLoaded == this.loaderInfo.bytesTotal) {

removeEventListener(Event.ENTER_FRAME, loadComplete);
this.loaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProgress);
gotoAndStop(2);
}
}

this.loaderInfo.addEventListener(Event.COMPLETE, loadComplete);
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);

function loadProgress(e:ProgressEvent):void {
preLoaderPercent = e.bytesLoaded / e.bytesTotal;
preloader.percentText.text = Math.ceil(preLoaderPercent * 100) + "%";
}

Basically this is a normal preloader with one addition.. On each frame (line 5) the function loadComplete is called. This function is SUPPOSED to be called when ProgressEvent.PROGRESS (line 19) is fired. Anyway, loadComplete (line 7) checks to see if the number of bytes loaded is the actual size of the file. This is basically a failsafe in the case that ProgressEvent.PROGRESS  fails to fire!

What a weird bug.. Anyway, it works!

Actionscript 3 fun…

So basically I have been annoyed by the fact that it is so hard for new users to find answers to simple everyday problems when using Actionscript 3 for flash development. This is especially true when transitioning from Actionscript 1 or 2.

I would like to use this forum to share some of the knowledge that I have gained over the past few years working with as3 and hopefully help some people skip over some of the frustration and pitfalls that I suffered through.

Stay tuned!

-Rick