Preloading into levels

| 11 Comments

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.

11 Comments

Their is a simple solution to the delema without the use of empty movie clips to associate preLoading code. I too was faced with the incompatability issue, at first, between browsers and a stand-alone Flash Player environment. The working model developed not only works but holds up when tested against different browser types. ...Going back to the basics. Simply, it was the passing of variables between the _level2 and _level0 movies (for example) with use of the getBytesLoaded and getBytesTotal functions. If the preloading code is placed within _level2, calling a progression status from a loader bar placed within _level0, it will not only load on demand but will give a visual representation of its progress. I have tested this in Flash Player 5, MX, 7, and it works the same in each.

I had initially gained alot of input from this original thread. I will be happy to share the code used and working models to show how it was developed. Thanks again for a great starting point to resolve my delema.

Shannon-

I would be interested in seeig tha code that you used- could you email me sample files?

I have been banging my head against the preload issue for weeks.

Thanks

Hi Darron.

Can you send me the sample file? I can't make it works.

Thank you so much!

William

I would also be interested in the code you used. If you could send it, I would be much appreciated.

Thanks,
Chris

William - I, as well am having difficulty with this preleoader issue when using layers. Would love to see the code. Appreciative, Victor.

i'm so glad to find a possible solution to my problem. Can you send me the code.Thanks i appreciate.Caroline.

Is it too late to jump on the bandwagon here? The code sounds like it would be really helpful =) Thanks!

I'm facing a similar issue, pls send me the code.........

yup me too.

I'm surprised that this page is getting so many visits lately.

The issue I ran into seemed to be some sort of configuration problem with my system. Most of the people I asked had no problems with _levelN.getBytesLoaded reporting useful information. So, this post is pretty much meaningless since _levelX.getBytesLoaded works as expected and was inspired by what appeared to be a bug caused by my environment that's not very reproducable.

Shannon's solution (the first comment) was to create a preloader for each movie clip that needed to be loaded, and then load that movie onto a level. You then don't have to worry about testing the level to preload, as the movie being loaded has it's own preloader. Because of that, you don't test _levelN for it's bytesloaded value, but the preloader in the clip being loaded runs just fine. If you want to do something like that, you can use code like this on the first frame of a clip being loaded:

stop();
function waitForLoad() {
// the target loading movie clip is referenced
// by the "ref_mc" variable
bytesLoaded = this.getBytesLoaded();
bytesTotal = this.getBytesTotal();
percent = Math.floor(bytesLoaded/bytesTotal * 100);

if (bytesLoaded == bytesTotal && bytesTotal > 4) {
trace("done loading");
delete this.onEnterFrame;
// start the movie
this.gotoAndPlay(2);
}
}
this.onEnterFrame = waitForLoad;

Hopefully that'll help everyone who's been asking for code. If you're using IE for the Mac my code blocks don't seem to appear in the original post - view the source of this page to see all of the code I posted, or switch to Safari.

send me preloader

Leave a comment



About this Entry

This page contains a single entry by darron published on August 13, 2003 6:40 PM.

Flash MX method chaining was the previous entry in this blog.

SharpFlash, back in the mix is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Archives

OpenID accepted here Learn more about OpenID
Powered by Movable Type 5.02