Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

  • Print
  • + Share This
  • 💬 Discuss
From the author of

The onLoadProgress() Method Works After All

Flash MX 2004 comes equipped with a few new ActionScript class files, and they're pretty darn cool. The MovieClipLoader class, for example, enables us to write a preloader that has almost no file weight (which is essential for a good preloader, for obvious reasons) and is without loop statements. You can then turn the thing into a component and never write a preloader script again.

One problem, though: The onLoadProgress() method of the MovieClipLoader class (the part used to trigger an animation for a progress bar, or something like that) does not fire when you are testing it on your computer. To test it, it's often thought that you have to upload the file with the preloader in it to a Web server, along with a .JPG or .SWF to load into the movie, and test it from there. From a Web server, it works perfectly.

TIP

Instead of posting the movie with the preloader script in it to the server and reposting it every time you make a change in the code so you can test it, try this: Post one .JPG image to the server and use the loadClip() method to call the absolute URL. Leave the image on the server permanently so you always have a quick way to test your preloader scripts.

For example, the preloader script below is complete. All you need on the Stage is a dynamic text field using info_txt as its instance name. (The boldface lines in the script are the ones I'm talking about.)

var my_mcl:MovieClipLoader = new MovieClipLoader();
var my_listener:Object = new Object();
my_listener.onLoadError = function (mc) {
  info_txt.text = "Error. An image did not load.";
};
my_listener.onLoadProgress = function (mc, loadedBytes, totalBytes) {
  info_txt.text = Math.floor(loadedBytes/totalBytes * 100) + "%";
};
my_listener.onLoadComplete = function(mc){
  info_txt.text = "";
}
my_mcl.addListener(my_listener);
my_mcl.loadClip("image.jpg", 1);

This script is supposed to display the percentage of bytes loaded for a loading asset. In this case, the loadClip() method loads an image called image.jpg into level 1. When you run it on your own computer, however, loading a file that is also on your computer, the onLoadProgress() method never runs, so the percentage counter doesn't display and you have no way to know that the script is working. To solve this (ensuring that you can test the script locally), change the URL parameter in the loadClip() method, like so:

my_mcl.loadClip("http://mydomain.com/misc/image.jpg", 1);

Loading the asset from the server triggers the onLoadProgress() method, and everything runs like it should. Problem solved.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus