AS3: recreating onLoad, MovieClip.addFrameScript part duex September 25, 2007
Recreating onLoad with MovieClip.addFrameScript
AS2 had an onLoad event, called after the frame's children had been created, which is largely how custom components were able to manipulate the visual state for useful things like:
- register listeners.
- hiding and showing clips (especially when multiple clips are stacked on top of each other for different error messages)
- jumping to a frame,
- setting up modal dialogs/mouse opaque areas
In AS3 this doesn't work the same, and this bites you on the occassion you have an actionscript class is registered to a class via the linkageID, like that of the "Document class.
If you try it you get the warning:
Warning: 1090: Migration issue: The onLoad event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'load', callback_handler).
So you think, ok, easy enough. Just add an eventListener to load. Test movie and then nothing happens. Search the help docs, and you find zero references to 'load' event. Check DisplayObject and you find every other event (render, added, etc) than the one we are looking for. Leads to one of two possibilities. One the load event doesn't exist... or second the load event has already been called prior to the class constructor being called, so adding a reference to it won't actually do anything. I can't really tell, I've tried searching and running some tests, and can't get it to work... which leads me to the question, just how do I accomplish all those in items above?
Thankfully the fix is easy: addFrameScript in the constructor.
addFrameScript(0,onLoadHandler); //works!!!
// addEventListener("load", onLoadHandler); // doesn't work
While on the subject, in order to access clips on the timeline from the *.as they should be public variables like this:
- public var my_btn:SimpleButton
- public var my_playerControls:MovieClip
and your class should be dynamic e.g
dynamic public class MyView extends MovieClip
Keeping the scripts already on the timeline.
Since your replacing the script call when it hits frame0, whatever is on the timeline won't be called. Thankfully since MovieClip is a dynamic class, scripts on the timeline are appended to the prototype, thus available by this which refers to the prototype chain instead of the class inheritance (Flash is multiple inheritance!). This is useful when the timeline has some configuration data/setup on it, and this approach allows you to call it prior or after your onLoadHandler
public function MyMovieClipClass(){
super();
addFrameScript(0,onLoadHandler);
// addEventListener("load", onLoadHandler); //doesn't work
trace(" AAAA CONSTRUCTOR AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
}
function onLoadHandler():void{
trace("BBBBB onLoadHandler BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
trace("onLoadHandler " + currentFrame);///pre frameactions
if(!this["frame1"]){
this["frame1"]();
}//post frameactions
my_btn.addEventListener(.....
my_playerControls.gotoAndStop("buffering");
}
OUTPUTS
- AAAA CONSTRUCTOR
- BBBB onLoadHandler
- CCCC frame1TimelineScripts

1 responses to 'AS3: recreating onLoad, MovieClip.addFrameScript part duex'
Update, when calling into a clip with frame actions, from another MovieClip, it appears that the last hack won’t work. Maybe a security/namespace fix. But an alternative is putting the config object on the clips parent timeline…more likely to get broken this way, but in some ways easier to update to different difficulties for t same clip.
Add a Comment: