15.11. Performing Actions When the Asset Is Loaded

Problem

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

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).

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 ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.