I ran into something recently that I've never come across before when dealing with preloading content onto a level in Flash MX. You can't reference a level that has no loaded content, thus making it impossible to preload with meaningful messages (% loaded, bytes loaded, etc).
I've always managed my loading assests with depths and run preloaders calling getBytesLoaded and getBytesTotal on the "containter" movieclip that the content is being loaded into. A quick code example is in order.
function waitForLoad() {
// the target loading movie clip is referenced
// by the "ref_mc" variable
bytesLoaded = this.ref_mc.getBytesLoaded();
bytesTotal = this.ref_mc.getBytesTotal();
percent = Math.floor(bytesLoaded/bytesTotal * 100);
if (bytesLoaded == bytesTotal && bytesTotal > 4) {
trace("done loading");
// implicitly deletes the onEnterFrame loop when
// the movie clip is removed
this.removeMovieClip();
}
}
// create a holder movie clip toload the external content into
createEmptyMovieClip("img_mc", 1);
// create a temporary movieclip to attach the frame loop
// to which checks for the loading progress of the
// movie clip referred to by "ref_mc"
createEmptyMovieClip("load_img_mc", 10);
load_img_mc.ref_mc = img_mc;
// assign the frame loop to be the preload function
load_img_mc.onEnterFrame = waitForLoad;
// load the external content
img_mc.loadMovie("external.swf");
I've never had an issue with code like this. However, if you want to use levels to load content into instead of depths (switch the loadMovie to loadMovieNum), the following code won't work:
trace(_level4.getBytesLoaded());
// "undefined", instead of an integer
However, once a .swf is loaded into a level, then the above code will return an integer value. It seems as though the _level reference only exists after the load is complete. I'm not sure that I understand why, and I didn't test any of this with the new Flash Player 7 beta floating around...
Still though, you can preloaded content into levels, but you can't report back loaded status. The preload code is actually a little easier... it would look like this:
function preloadLevel() {
if (_level5 != undefined) {
trace("level 5 loaded");
delete this.onEnterFrame;
}
}
_root.onEnterFrame = preloadLevel;
loadMovieNum("external.swf", 5); // load something into level 5
This isn't a bug - it works as described (from the Flash Reference). "Once a movie is loaded into a level, you can use the syntax, _levelN, where N is the level number, to target the movie." It makes no mention of using the level reference beforehand.
So... if you're having trouble getting a preloader to work with levels, perhaps now you have an idea why.
EDIT - 10/28/03 - Thanks to Muzak, I need to point out that you can get the % loaded from a level, but only in certain versions of the Flash Player. On Windows, when viewing the .swf through a browser that doesn't use the Active-X control (like Mozilla), the % loaded comes through just fine. However, viewing the .swf in the Flash Authoring Environment, or in the Stand-alone Flash player, or with Internet Explorer, the getBytesLoaded function will not give the % loaded.