User Guide
604 Chapter 7: ActionScript for Flash
Parameters
None.
Returns
Nothing.
Description
Event handler; invoked when the movie clip is instantiated and appears in the Timeline. You
must define a function that executes when the event handler is invoked. You can define the
function on the Timeline or in a class file that extends the MovieClip class or is linked to a symbol
in the library.
This handler can be used only with movie clips for which you have a symbol in the library that is
associated with a class. If you want an event handler to be invoked when a specific movie clip
loads, for example when you use
MovieClip.loadMovie() to load a SWF file dynamically, you
must use
onClipEvent(load) instead of this handler. The latter handler is invoked when any
movie clip loads.
Example
The following example illustrates one way to use MovieClip.onLoad() with setInterval() to
check that a file has loaded into a movie clip that’s created at runtime:
this.createEmptyMovieClip("tester_mc", 1);
tester_mc.loadMovie("http://www.yourserver.com/your_movie.swf");
function checkLoaded(target_mc:MovieClip) {
var pctLoaded:Number = target_mc.getBytesLoaded()/
target_mc.getBytesTotal()*100;
if (!isNaN(pctLoaded) && (pctLoaded>0)) {
target_mc.onLoad = doOnLoad;
trace("clearing interval");
clearInterval(myInterval);
}
}
var myInterval:Number = setInterval(checkLoaded, 100, tester_mc);
function doOnLoad() {
trace("movie loaded");
}
The following example displays the equivalent of the previous ActionScript example:
this.createEmptyMovieClip("tester_mc", 1);
var mclListener:Object = new Object();
mclListener.onLoadInit = function(target_mc:MovieClip) {
trace("movie loaded");
};
var image_mcl:MovieClipLoader = new MovieClipLoader();
image_mcl.addListener(mclListener);
image_mcl.loadClip("http://www.yourserver.com/your_movie.swf", tester_mc);
See also
onClipEvent()
, MovieClipLoader class