Home arrow Flash arrow Page 3 - Building Preloaders
FLASH

Building Preloaders


A preloader prevents a Flash movie from playing back until the movie can stream properly. Without a preloader, you may find that your movie tries to play frames that haven't been loaded yet, leading to unacceptable playback behavior. This article explains how to build a preloader. It is excerpted from chapter 20 of the Flash 8 Cookbook, written by Joey Lott (O'Reilly, 2006; ISBN: 0596102402). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 16
February 22, 2007
TABLE OF CONTENTS:
  1. · Building Preloaders
  2. · 20.1 Determining How a Movie Will Download
  3. · 20.2 Building a Simple Preloader
  4. · 20.3 Building a Preloader that Displays Load Percentage
  5. · 20.4 Using a Progress Bar to Create a Graphical Preloader
  6. · 20.5 Creating Preloaders for Files with Exported Symbols
  7. · 20.6 Creating Preloaders for Files with Components

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Building Preloaders - 20.2 Building a Simple Preloader
(Page 3 of 7 )

Problem

You want to create a basic preloader, which will ensure smooth playback of a movie.

Solution

Attach a preloader script to the first frame of the movie.

Discussion

To add a simple preloader to any movie, attach the following script to the first frame of the timeline:

  stop();
  var nPreloaderInterval:Number = setInterval(this, "checkPreloader", 100);

  function checkPreloader():Void {
   
var nLoadedBytes:Number = this.getBytesLoaded();
   
var nTotalBytes:Number = this.getBytesTotal();
   
if(nLoadedBytes >= nTotalBytes) {
     
clearInterval(nPreloaderInterval);
   
play();
   
}
  }

This script stops the playhead in the first frame, preventing further playback, until the preloader has verified that the entire SWF has loaded. It then uses an interval function to poll the SWF for the download progress every 100 milliseconds or so. When the number of downloaded bytes equals the total number of bytes, it knows that the file has downloaded entirely. At that point, it stops the interval and plays the timeline.

From a functional standpoint, this script accomplishes what it sets out to: it guarantees the smooth playback of the movie. However, it communicates none of this to the user. That is, while the script is busy determining whether the movie has fully loaded, the user sees only the content on frame 1. At a minimum, you should also put a message in frame 1 that indicates that the movie is loading and will begin playback when it has loaded. Users will have no way of knowing how long they will have to wait, but at least they won’t initially assume the movie is broken. Place the contents of the main movie beginning on frame 2 or another frame, which you can specify by substituting a gotoAndPlay( ) action in place of the play( ) action.

To test the functionality of the preloader, add it to a Flash document with a large bitmap, sound, or other asset on frame 2 or later and test the movie (Control -> Test Movie). Be sure to choose View -> Show Streaming for the full effect.

See Also

Recipe 20.1, Recipe 20.3, Recipe 20.4


blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials