AS1 OOP: Custom Classes with MovieClips
         by senocular  

MovieClip.onLoad
For movieclips, the onLoad event is called for when a movieclip fully loads itself in onto the timeline it was placed. It is important to note that this onLoad is not necessarily the same as onClipEvent(load) which you may have used before. The use of onLoad is best in this circumstance of class inheritance with attached movieclips. Use elsewhere can cause you and may have already caused you some problems. For example, here’s a list of common issues encountered with onLoad misuse.

  • Intuition tells us that to initiate values in a movieclip after using attachMovie to place it from the library on the screen, the onLoad would be used. This is in fact not the case. The movieclip is technically already loaded by the time the line following attachMovie is executed by Flash. That being the case, not only would an onLoad not be called (it’s already loaded), but anything needed to be done for that clip can just as well be added directly following the attachMovie command.
     
  • Somewhat similarly, you have movieclips on the timeline. What if you wanted to know when movieclipA has appeared on the timeline? One might think assigning an onLoad for “movieclipA” would be able to tell us that. The problem there is that the onLoad will not be defined for a movieclipA if movieclipA does not yet exist. Of course, at that point, an onLoad would have already been called anyway.
     
  • Often is the case that you would wish to be notified when a movieclip has completed loading information, such as a jpeg or another swf, from an external source. One would think an onLoad event would suit this situation nicely, when the truth is, it does not. The reason for this is that in loading an external swf or jpeg into a movieclip Flash clears that movieclip of all variables and properties. This includes functions as well – functions such as any defined onLoad. Having been cleared, the onLoad would then of course not be called as would be expected after the requested swf or jpeg has fully loaded.
     
  • On top of everything else, using onLoad in conjunction with onClipEvent(load) can cause the scripted onLoad to fire prior to when it would otherwise. This actually also applies to any script on a movieclip even it its just a double slash comment (//). The basic rule is to use either one or the other. Chances are, if you are using registerClass to associate your classes with movieclips, you are probably attaching the clips with attachMovie. That being the case, you aren’t even capable of assigning onClipEvent’s – which is a good thing. Otherwise, if not using attachMovie, you should try to avoid using scripts on movieclip instances in the timeline which are to be associated with a class through registerClass.

It’s the timing of onLoad that makes it so useful – and in saying onLoad here, it’s meant as a shared method of a class accessible to all class instances (i.e. a prototype method in a class). A class’s constructor is called for an instance immediately in the initialization process of a movieclip. At that point, no internally defined values have been set. The onLoad event gets called for a movieclip afterwards allowing such internal definitions to occur. Here is a list of operations as the occur in the initialization of a class-registered movieclip at the point of creation with attachMovie.

1. attachMovie’s init object assignment
2. class constructor
3. remaining frame script in the main (attachMovie’s) timeline i.e. the init method if used
4. attached movieclip’s timeline frame scripts
5. onClipEvent(load) of any clips within the attached movieclip
6. timeline frame scripts of any clips within the attached movieclip
7. class onLoad *

   

* If the clip is not created with attachMovie and is instead placed on the timeline in Flash with any script on the clip, as previously mentioned, onLoad will actually be called directly after the point in time which onClipEvent(load), if used, would be called which is directly after the 2) class constructor.

In the non-onClipEvent interupted order, you can see that onLoad is the last event called in the series of events following the movieclip’s creation. This is important because the constructor call, as you can see, comes before any of the attached clip' interior clips code gets called. This means that any initialization of those clips (especially components) will not be available to the constructor call. For that availablity, you would use want to use an onLoad method in your class. The constructor would be used for immediate setup and onLoad for follow-up. For a house, the constructor details the blueprints. The house is then built (movieclips loaded internally) and the onLoad would be the final decision of a family to finally move in.

Consider an attached movieclip with a scrollbar in it. We can make a class for that movieclip, but in order to access methods defined for the scrollbar within the movieclip, you would have to wait until onLoad.

Example:

[ scrollbar accessible only after onLoad event ]

 

LoremScroller = function(){
// text for a textfield instance can be set
this.lorem_txt.text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit...";
// but methods of a scrollbar component
// will not work (not yet defined)
output_txt.text += "constructor: ("
output_txt.text += this.loremScrollbar.getScrollPosition() + ")\n"; // undefined
};
LoremScroller.prototype = new MovieClip();
LoremScroller.prototype.onLoad = function(){
// methods for internal components have been
// defined by the time onLoad is called
output_txt.text += "onLoad: ("
output_txt.text += this.loremScrollbar.getScrollPosition() +")"; // 1
};
Object.registerClass("lorem", LoremScroller);

 
this.attachMovie("lorem", "loremTextBox1", 1, {_x:150, _y:130});

download source

 

Prev Page
 



SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.