User Guide

Preloading external media 301
Preloading SWF and JPEG files
To preload SWF and JPEG files into movie clip instances, you can use the “MovieClipLoader
class”. This class provides an event listener mechanism to give notification about the status of file
downloads into movie clips. Using a MovieClipLoader object to preload SWF and JPEG files
involves the following steps:
Create a new MovieClipLoader object You can use a single MovieClipLoader object to track
the download progress of multiple files, or create a separate object for each file’s progress. Create a
new movie clip, load your contents into it, and then create the MovieClipLoader object.
this.createEmptyMovieClip("target_mc", 999);
var loader:MovieClipLoader = new MovieClipLoader();
Create a listener object and create event handlers
The listener object can be any
ActionScript object, such as a generic Object object, a movie clip, or a custom component.
For example, the following code creates a generic listener object named
loadListener and
defines for itself
onLoadStart, onLoadProgress, and onLoadComplete functions:
var loader:MovieClipLoader = new MovieClipLoader();
// Create listener object:
var loadListener:Object = new Object();
loadListener.onLoadStart = function(loadTarget) {
trace("Loading into "+loadTarget+" has started.");
};
loadListener.onLoadProgress = function(loadTarget, bytesLoaded, bytesTotal) {
var percentLoaded = bytesLoaded/bytesTotal*100;
trace("%"+percentLoaded+" into target "+loadTarget);
};
loadListener.onLoadComplete = function(loadTarget) {
trace("Load completed into: "+loadTarget);
};
Register the listener object with the MovieClipLoader object
In order for the listener object
to receive the loading events, you must register it with the MovieClipLoader object, as shown in
the following code:
loader.addListener(loadListener);
Begin loading the file (JPEG or SWF) into a target clip To start the download of the JPEG or
SWF file, you use the
MovieClipLoader.loadClip() method, as shown in the following code:
loader.loadClip("mymovie.swf", target_mc);
Note: You can use MovieClipLoader methods only to track the download progress of files loaded
with the
MovieClipLoader.loadClip() method. You cannot use the loadMovie() function or
MovieClip.loadMovie() method.
The following example uses the setProgress() method of the ProgressBar component to
display the download progress of a SWF file. (See “ProgressBar component” in Using
Components.)