View comments | RSS feed

MovieClip.onLoad

Availability

Flash Player 6.

Usage

my_mc.onLoad = function() {
   // your statements here
}

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. For more information, see "Assigning a class to a movie clip symbol" in Using ActionScript in Flash.

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


Comments


recoveredfromflashMX2004 said on Aug 4, 2004 at 3:28 PM :
Chris.Velevitch said on Feb 15, 2004 at 4:14 PM :

If the swf file replacement.swf has an onLoad function in it, will that onLoad function be run when the movie is loaded?
webweber@bluewin.ch said on Nov 5, 2004 at 1:23 AM :
There is a difference in how different Flash Players support onLoad.

The Flash Player for IE supports onLoad defined on _root. The Mozilla Player does not support it, the handler defined on _root is never called.
(Tested for the Player Version 7)
No screen name said on Apr 26, 2005 at 4:07 PM :
The line of code
target_mc.onLoad = doOnLoad;
Won't call the function doOnLoad. It should read
target_mc.onLoad = doOnLoad();

Thanks
Francis Cheng said on Apr 27, 2005 at 3:11 PM :
Actually that first example in the examples section doesn't show the correct usage of this event handler. Please disregard it. The MovieClip.onLoad() event handler is designed for use within class definitions, and will not work if defined outside of a class definition.

This example shows you how to use the onLoad event handler in an ActionScript 2.0 class definition that extends the MovieClip class. First, create a class file named Oval.as and define a class method named onLoad() and make sure that the class file is placed in the proper class path:

class Oval extends MovieClip{

public function onLoad () {
trace ("onLoad called");
}

}

Second, create a movie clip symbol in your library and name it Oval. Context-click (usually right-click) on the symbol in the Library panel and select Linkage... from the pop-up menu. Click on "Export for ActionScript" and fill in the "Identifier" and "ActionScript 2.0 Class" fields with the word "Oval" (no quotes). Leave "Export in First Frame" checked and click OK.

Third, go to the first frame of your file and enter the following code in the Actions Panel:

var myOval:Oval = Oval(attachMovie("Oval","Oval_1",1));

Finally, do a test movie, and you should see the text "onLoad called" in the Output Panel.
good-question said on May 26, 2005 at 9:48 AM :
The comment above with respect to adding the call operator [ () ] to the doOnload is incorrect - the function is not being called, rather being assigned to the onLoad event of the movieclip.

Of course, based on the comments from MM above, it's a moot point as it won't work anyway.
jaredly2 said on Jul 7, 2005 at 2:50 PM :
This Function does not seam to work at all. Has any one had any luck?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001534.html