One little bit of explanation for the swonking function in that .fla I posted. My comments were a bit brief in the code, so here’s a fuller explanation. Who knows, maybe this will be useful to somebody.
Consider this code:
// create an empty holder clip
this.createEmptyMovieClip("holder_mc", 0);
// load an image or clip into holder_mc
this.holder_mc.loadMovie("myClip.swf", 1);
// check how much is loaded
var bytesLoaded = this.holder_mc.getBytesLoaded();
var bytesTotal = this.holder_mc.getBytesTotal();
if (bytesLoaded < bytesTotal) {
trace("all bytes are not yet loaded");
} else {
trace("all bytes are loaded");
} // end if (bytesLoaded < bytesTotal)
Interestingly enough, this code will trace “all bytes are loaded”. That is, bytesLoaded < bytesTotal will evaluate to false. If you trace bytesLoaded and bytesTotal, you’ll see, however, that both are equal to 0. bytesLoaded will obviously equal 0, but bytesTotal equalling 0 is unexpected. In any case, the if-statement’s condition evaluates to “if (0 < 0)”, which is false.
If, on the other hand, you execute the if-statement on the next frame (or if you use a setInterval() to execute the if-statement again a few milliseconds later), then bytesTotal does not equal 0; rather, it equals the total amount of bytes of the loading clip/image. That’s the number we’re after, not 0.
The conclusion to be drawn is that the first time around the loading process has not initialized yet. The parser executes the if-statement so quickly that the loading process doesn’t have time to start.
This is because all loading processes in Flash are asynchronous: the parser doesn’t wait for the loading to be completed before it moves to the next line of code. Instead, it sends a “load” call to the Flash Player to start loading, but the parser in the meantime just keeps stepping through the code. Hence, the if-statement gets executed before the Flash Player really gets a chance to initialize the loading process.
To counter this you have to do what is informally called “swonking”, checking over and over again whether or not the movie is loaded. Since the first time around the swonking check returns a 0 for both bytesLoaded *and * bytesTotal, we have to accomodate for that first moment when both are equal at 0.
You’ll see in my code that the swonking function first tests whether or not bytesTotal == 0. If so, then the swonking function does nothing and it waits until the setInterval() executes the swonking function again. This will happen as many times as needed until bytesTotal finally does not equal 0 but rather equals the number of bytes of the loading clip/image. Only then does the swonking function start checking “if (bytesLoaded < bytesTotal)”.
It would be nice to just call an onLoad event handler for loading clips like we can for LoadVars or XML:
// load the image
this.holder_mc.loadMovie("myClip.swf", 1);
// do this when the movieclip loads
this.holder_mc.onLoad = function() {
trace("clip/image has loaded!");
} // end clip.onLoad()
Unfortunately, onLoad necessarily has to work differently for movieclips because if you call an “onLoad” as a method of a movieClip, then that movieClip must already be loaded (holder_mc.onLoad() would check whether holder_mc is loaded, which of course it is, since I called the onLoad function off the clip in the first place). The onLoad event handler for movieclips serves a different purpose than swonking. (Note: you can use onClipEvent(load) for swonking in the Flash 5 style if you’re privy to that, but I’m a pretty straight MXer)
In any case, swonking is more difficult for loading images and clips since the onLoad event handler for movieClips serves a different purpose and thus can’t be used to swonk. So we’re stuck with this strange “if bytes are all loaded” function. Hopefully the next version will have a nice event handler that’s native, something like “onLoadedInto()” or whatnot. For now, we’re stuck with checking the bytes loaded.
Hope this makes my code in the .fla more understandable.