Yet another Flash MX loader

An in depth look at building a reusable preloader for Flash MX.

click here to get more tutorials

01 Introduction
02 What should the loader do?
03 How should the preloader work? / What should it look like?
04 Useful reading / Designing the loader movie
05 What have we got so far? / Getting it to stop and play its parent
06 Setting a preload amount / Sync the loader movie with the download amount / Adding text feedback
07 Setting a maximum loader play rate / Setting a maximum load rate
08 Using the loader
09 Conclusion / About the authors / Copyright

Previous << Useful reading / Designing the loader movie

What have we got so far?

You should have a file (in our case "loader") with a movie in it ("loading") that is ready to be shared out to other Flash MX files.

If you preview your working file at this point you will see your loading animation looping on the stage.

The way we have designed things, we have met requirements 1, 4, 5, 9, and 14. And we have nearly achieved 2, 3, 6, 10, 12, and 13. Seven and eight will need to be proven, and I will probably end up leaving 11 as an exercise because I believe that this feature isn't actually useful most of the time.

Depending on how you set up your fonts, you can range from 2kb (using a _sans font) for your loader to around 6kb (using Arial and embedding font outlines for 0-9, k, b, o, and f in the Dynamic Text object). If your project warrants it, you can also create a shared library asset of your font of choice. See "Using Flash > Working with Text > Creating font symbols" in your help file for more info.

Getting it to stop and play its parent

Our loading movie will need to respond to load and enterFrame movie clip events. In the load event we will carry out some initialisation and in the enterFrame event we will check to see how our loading is going.

At this point it is probably useful to create a test bed movie to see how we are doing. This may be done with your original file or do a "Save As..." and name it something like "test-bed.fla". We will put the preloader movie on a layer named loader and place it only in the first frame. Create another layer that contains a large image (as we have done in the image layer below) and have it appear in frames 2 and 3. Also make a actions layer for assorted ActionScript and give that a keyframe in frame 3. Put a stop() command in that frame.

If you preview your test file (Ctrl+Enter), you should only see your image. Your preloader will just appear to be skipped.

Now close your test window and go back to your development windows. Go to frame 1 and select your loader movie. In the "Actions – Movie Clip" window, make sure you are in Expert Mode.

The first code to add will make the parent movie (the movie that we are loading) stop and play automatically. Let's add that code:

onClipEvent (load) {
        _parent.stop();
}

onClipEvent (enterFrame) {
        if (_parent.getBytesLoaded() >= _parent.getBytesTotal()) {
                _parent.play();
        }
}
      

First, the loader stops the parent from playing, then every frame it checks to see how the download is going. If it has loaded completely, the parent movie is allowed to play.

If you preview this, you won't see any difference unless you Show Streaming (Ctrl+Enter in the preview window). When you stream the content however, the parent movie will stop and wait for the whole file to load before continuing on.

For a better test, add a large sound file to the stage. Add a large sound sample to your library (if it is too small, your sound won't have the chance to stream properly for this test). Create another layer named "sound" and add a keyframe to frame 2. Now make your sound stream by adding it to your sound layer at frame 2, ensuring that you have enough frames to fully play your sound. Finally, drag your stop script and your image to display after your sound has finished streaming.

When you preview this with Show Streaming on, your loader will loop its animation until the whole file is loaded, then the movie will continue playing to the end. If you go back and remove the code from your loader movie in frame 1, you should see the loader briefly flash on the stage, then the play head stops until enough sound is loaded to play the first part of the sound. As the file continues to load, your sound will stream until it completes playing then stop again while the image finishes loading.

Save your file at this point.

Next >> Setting a preload amount / Sync the loader movie with the download amount / Adding text feedback

01 Introduction
02 What should the loader do?
03 How should the preloader work? / What should it look like?
04 Useful reading / Designing the loader movie
05 What have we got so far? / Getting it to stop and play its parent
06 Setting a preload amount / Sync the loader movie with the download amount / Adding text feedback
07 Setting a maximum loader play rate / Setting a maximum load rate
08 Using the loader
09 Conclusion / About the authors / Copyright

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