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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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!

Share on FacebookShare on Google+Share on StumbleUponTweet about this on TwitterEmail this to someone

Facebook comments:

4 thoughts on “actionscript 3 preloader freezes at 100% (SOLVED!)”

  1. Thank you so much. It took me hours of searching to find your solution and I am so greatful. I think I stumbled across it from a link on actionscript.org
    The problem I was having – I hadn’t put in and event listener for ENTER_FRAME. It seems so obvious now.
    Thank you
    Thank you
    Thank you
    🙂

Leave a Reply