AS1 OOP: Custom Classes with MovieClips
by senocular
#initclip and #endinitclip
For movieclips associated with classes, its important that
the classes for such movieclips are defined before those
movieclips attempt to access them (before they are created).
Generally that's not an issue since your classes should be
pre-defined in the first frame of your Flash movie. For
self-contained movieclips which contain their own classes
defined within themselves, i.e. components, this is not the
case.
Because components have their class
definition within their component movieclip, Flash has no
way of reading and defining that class until after the
component clip has already been instantiated. This means
that that particular component would then not be able to
have an association with that class (as it was not defined
in time for its creation). The #initclip and #endinitclip
actions allow for a solution.
Actionscript placed between the #initclip
and #endinitclip commands in a movieclip in a Flash movie's
library that has been exported for actionscript with a
linkage ID will be defined prior to frame 1 of that movie as
its loaded into the Flash player. This allows for class
definitions for components contained within components to be
predefined before that component can be used. Code outside
of this block will be run normally after that clip is loaded
and played in the player.
- #initclip
- // code to be defined prior to
- // frame 1 of the main movie
- componentClass = function(){
- // component's constructor
- };
- // more class definition for this clip
- // register the class with this movieclip symbol
- Object.registerClass("thisLinkageID", componentClass);
- #endinitclip
- // normally functioning code
- stop();
- // etc.
The practical use of #initclip and #endinitclip pretty
much begins and ends with components. Otherwise, your code
can just be placed in frame 1 of your main timeline and will
function properly, being defined in time for any movieclip
in your movie needing it. Because components contain their
own class definitions, they need these actions to be able to
pre-define their classes so that they will be ready when the
component is actually used.