Recipe 15.11 Performing Actions When the Asset Is Loaded

15.11.1 Problem

You want to define actions to occur when an asset has loaded completely.

15.11.2 Solution

For sounds, use the onLoad( ) event handler method. For all other datatypes, set a callback function using the setChangeHandler( ) of the FProgressBar class or the custom LoadMonitor class (depending on which technique you are using).

15.11.3 Discussion

The Sound class provides a convenient way to perform actions when the sound has loaded. Simply define an onLoad( ) method for the Sound object and Flash automatically invokes that method once the sound has loaded:

mySound_sound.onLoad = function (  ) {
  // When the sound loads, tell it to start.
  this.start(  );

Other types of assets require another way of determining when the asset has loaded. When using the Progress Bar component, you can define a callback function that is called each time there is progress in the loading of the asset. The setChangeHandler( ) method accepts the name of a callback function as a string. It also accepts an optional, second parameter specifying a reference to the object in which the function can be found. If the second parameter is omitted, the timeline in which the Progress Bar component exists is assumed. The callback function is automatically passed a reference to the Progress Bar component.

// Define the callback function.
function onProgress (cmpt) {
  trace("onProgress(  ) called for the progress bar " + cmpt);

// Set the callback function for updates on the progress bar named pBar.

Note that the callback function is called every time there is an update in the load progress for the asset being monitored. If you want to perform certain actions only when the loading has completed, use the FProgressBar.getPercentComplete( ) method to check whether the loading progress has reached 100%. For example:

function onProgress (cmpt) {
  // Ensure that the asset has completed loading before performing the actions.
  if (cmpt.getPercentComplete(  ) == 100) {
    trace("Loading complete");

If you are using the custom LoadMonitor class (see Recipe 15.10), you can set a callback function for the LoadMonitor object using the setChangeHandler( ) method. Like the change handler callback function for FProgressBar, the callback function for LoadMonitor is called each time load progress is made. Therefore, within the callback function, you should first determine whether the asset has completely loaded before performing any actions.

// Include the custom LoadMonitor class.
#include ""

// Define the callback function.
function resize (obj) {
  if (obj.isLoaded) {
    obj._xscale = 50;
    obj._yscale = 50;

// Create a movie clip and load an external .swf file.
this.createEmptyMovieClip("myMovieClip_mc", 1);

// Create the LoadMonitor object to monitor the progress of myMovieClip_mc and set
// the callback function.
lm = new LoadMonitor(myMovieClip_mc);

15.11.4 See Also

Recipe 15.9 and Recipe 15.10

    Part I: Local Recipes
    Part II: Remote Recipes