Setting a maximum load rate

The last bit of code will limit the number of frames that we can leap in a single bound. All we have to do is keep track of what frame we handed back on our last function call, compare it to where we want to go now, and if it is too much of an advance then we clamp it to a maximum jump.

To achieve this, we will need an extra variable named lastFrame to store the last frame we showed. This is initialised to 1 which is the frame just before our loader animation starts. Here is the final loader code:

lastFrame = 1;


function loadedIndicatorFrame() {
        ????var newFrame = int((_parent.getBytesLoaded() / _parent.getBytesTotal()) * 65) + 2;
        ????if (newFrame - lastFrame > 4) {? //too far
                ????????? lastFrame += 4;
                ????????? loadedText = int(_parent.getBytesTotal() / 1024 * (lastFrame - 2) / 65) + "kb of " + int(_parent.getBytesTotal() / 1024) + "kb";
                ????} else if (newFrame - lastFrame > 0) {? //normal move
                ????????? lastFrame++;
                ????????? loadedText = int(_parent.getBytesLoaded() / 1024) + "kb of " + int(_parent.getBytesTotal() / 1024) + "kb";
                ????}
        ????return lastFrame;
}

Most of the code is the same, but I'll give it a quick run through anyway. Given that our frame rate for the Western Australian Museum project was set to 25 fps, we found that skipping 4 frames at a time gave us enough time to see the loader and still not be annoying if a movie was already downloaded. Just play with the values to get it right for your frame rate.

If we are skipping frames for a movie that has already been downloaded, then we want to fake our bytes loaded text so that it matches the current frame that we are showing. Our loadedText calculations are now based on lastFrame instead of getBytesLoaded.

Preview now (Ctrl+Enter) to see your loader animation play through once in a fast-forward style before continuing on with the parent movie. When streaming the preview, you should see your loader animation tick through according to your bandwidth settings until you hit the required percentage, then it will continue on playing the parent movie to the end.

If you find that you are creating large SWF files (such as for video) you might find that you need to update the Dynamic Text object more frequently than when you advance to the next frame of your loader. The code for this would be:

lastFrame = 1;


function loadedIndicatorFrame() {
        ????var newFrame = int((_parent.getBytesLoaded() / _parent.getBytesTotal()) * 65) + 2;
        ????if (newFrame - lastFrame > 4) {? //too far
                ????????lastFrame += 4;
                ????????loadedText = int(_parent.getBytesTotal() / 1024 * (lastFrame - 2) / 65) + "kb of " + int(_parent.getBytesTotal() / 1024) + "kb";
                ????} else if (newFrame - lastFrame > 0) {? //normal move
                ???????lastFrame++;
                ????????loadedText = int(_parent.getBytesLoaded() / 1024) + "kb of " + int(_parent.getBytesTotal() / 1024) + "kb";
                ????} else {? //update the text only
                ????????loadedText = int(_parent.getBytesLoaded() / 1024) + "kb of " + int(_parent.getBytesTotal() / 1024) + "kb";
                ????}
        ????return lastFrame;
}

Now would be a good time to save this using Save As... so that you can give your loader a better name (choose "loader" again). It would also be good to get rid of any test images, sounds, and layers from your file. After tidying up the file, check the linkage on your loading movie in the Library for loader.fla by right-clicking on it and selecting the Linkage... option. It should look like this:

Save it, publish it, and we're ready to rock and roll.

Using the loader

All right, let's see how easy this sucker is to use!

Create a new file and open up your Library window (leave your loader.fla file open as well).

Drag your loading resource from loader.fla to your new movie. All the linkage will be set up correctly for you, but you can check it if you want. You should see that it has been set up to "Import for runtime sharing" from loader.swf.

Create some layers and add resources like you did for the test-bed.fla movie. Make sure you put a stop script in the last frame. I ended up with this:

Now go copy the code from your loader resource and paste it on to your loader in your new movie. Change your preload percent if you want.

That's it! Save and publish. Nothing more to do.

Conclusion

In building the virtual exhibition for the Western Australian Museum, we made extensive use of the loader. With over 360 SWF files to load we needed a resource that would not be a large overhead on the bandwidth, would be easy to add to a movie, and could handle custom preload amounts.

The way we approached most of our files was to load all window, caption, and text elements as early as we could, then when we hit the larger elements, like images and sounds, we would use the loader to indicate progress. Because we can put it anywhere we like, we can make the best use of the streaming capabilities of Flash MX and just when things start to stall (when it hits a large image or audio sample) we whack in a loader so that the site visitor still sees something happening.

Having a loader does not mean that you can get away with developing bloated Flash MX sites: great big monolithic monsters have gone the way of the dinosaurs. Be clever and stream as much content you can in an entertaining way. At some point, though, you will probably need a loader when the media you want to show gets too big. This approach should serve well for most occasions.

About the authors

The Glasson Murray Group, Pty. Ltd. creates and presents high quality and engaging content for delivery across a range of media. They designed and developed the virtual exhibition in conjunction with the Western Australian Museum, producing a truly compelling and unparalleled presentation.

http://www.gmg.com.au/

Copyright

Materials are copyrighted and are protected by worldwide copyright laws and treaty provisions. Copyright law in Australia is contained in the Copyright Act 1968 (Cth) and in decisions of courts, with further amendments in the Copyright Amendment (Moral Rights) Act 2000. They may not be copied, reproduced, modified, published, uploaded, posted, transmitted, or distributed in any way, without GMG's prior written permission. Except as expressly provided herein, GMG and its suppliers do not grant any express or implied right to you under any patents, copyrights, trademarks, or trade secret information. Other rights may be granted to you by GMG in writing or incorporated elsewhere in the Materials.

? 2003 Glasson Murray Group Pty Ltd (ACN 098 651 542), Western Australia. All rights reserved.