AS1 OOP: Custom Classes with MovieClips
         by senocular  

Introduction
Classes can be created not only to hold and handle data and information as scripted objects residing in memory, but also to pose as controllers for movieclip objects on the screen. What this means is that you can create a custom class in Actionscript that can be used as the primary class of a movieclip.

By default, all movieclips on the screen are instances of the type MovieClip. They are based on the MovieClip class in Actionscript and inherit methods and properties from the MovieClip prototype (properties like hitTest and getDepth etc.). To have your own classes work with movieclip instances on the screen, you would want to have them extend the MovieClip class.

There is only one little problem. Movieclip instances aren't created like other object instances using new keyword in combination with a class constructor. They are created internally by Flash as they not only exist as containers for information memory, but also as visual elements on the screen. That requires more goings on than what's required with normal object creation. Flash needs to get down and dirty with the library and copying visual symbols on to the timeline and so on. Because of that, your movieclip extending class constructors can’t exactly be used to make a movieclip instance in Flash. Those movieclips are only made by Flash itself as commanded by the timeline or through the use of dynamic movieclip creation functions like duplicateMovieClip or attachMovie. So what is there to do?

Luckily, Actionscript provides you a method for telling Flash that it may need to use a class other than MovieClip when it creates movieclip instances on the screen. That way every movieclip won’t be restricted to the same default inheritance chain with the direct association to the MovieClip class and you can have your own class pose as a movieclip instance's primary class. The command that allows for this is Object.registerClass.

 

 
 



SUPPORTERS:

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