Download the source code
Introduction
Veteran 4GL programmers, OO Framework gurus, and other geeks far and wide would gasp and pale if they knew what implications the title of this article brought with it. Given the combination, and the often over trusted Object Oriented promises of ActionScript 2.0, anyone with a solid programming background would be a bit surprised, at the very least, by the behavior of this 'MovieClip Monster'.
The reasoning behind giving the MovieClip a 'Monster' status is because the MovieClip object does not behave like any other OO Class and due to it being at the core of Flash, and ActionScript Programming , I would imagine that it has caused it's share of headaches to more people than just me. Don't get me wrong, I am a hardcore ActionScript programmer, I've hung in there by a thread, and now that ActionScript 3.0 has finally arrived with Flash CS3, I can breath a sigh of relif. OO ActionScript has finally arrived, and none too late!
There is a lot of power in Extending MovieClip . It gives your clip clout, demanding respect from all of the lesser MovieClips on the stage. It also offers the developer more flexibility, and opens up a world of potential for your next ActionScript Flash Application. With this power and respect, comes the weight of responsibility.
I hereby take responsibility to share with you all of the lessons that I've learned about this subject so that you will know what to watch out for as you grow your own skill set. I hope that you will gain encouragement and momentum from this knowledge, and I wish you a journey full of epiphanies, as you develop your ActionScript Programming skills going forward.
My Lessons Learned
The biggest challenge with Extending MovieClip , is that it does not use it's constructor to instantiate, as other classes are wont to do. Here are my lessons learned listed for you in the hopes that they save you some stress as you develop your ActionScript Programming skills.
- The MovieClip and it's SubClasses Construct via PARAMS Object that gets passed into another MovieClip's attachMovie method
var PARAMS:Object = new Object();someMc.attachMovie(subClassMcLinkageName, subClassInstanceName, someMcDepth, PARAMS);
- The PARAMS object property names correspond with your SubClass property names.
- Initial values must also be set to the PARAMS object properties, these are the values that correspond with the properties of the SubClass that you wish to populate upon this, pseudo instantiation.
- The object, so cleverly named, 'PARAMS', must get passed into the subclass via another MovieClip's attachMovie method in order for the SubClass MovieClip to initialize correctly.
Rules For Constructing a SubClass of MovieClip:
- YOU SHOULD : Pass a PARAMS Object into the MovieClip's SubClass for instantiation using another MovieClip's parameters argument.
some_mc.attachMovie(linkageName, instanceName, depth, PARAMS);
- YOU SHOULD NOT : Pass Constructor Arguments Into a MovieClip SubClass using the traditional constructor-argument type of instantiation:
SubClass instance = new SubClass(parm1, parm2...);
Tools and Resources
You will find the working demonstration of the code I've discussed at the beginning of this article.
I have put together a zipped up source code file help clarify the concept for anyone who may need it. You can find it at Flash.Tips-for-You.com . Upon reviewing the demonstration source code, you will find:
- The first example passes a value into the constructor of an ActionScript 2.0 class that is extending MovieClip (invalid and improper).
- It results in undefined properties.
- The, visible, Monster Class had a PARAMS object passed into it.
- The PARAMS object had the numberOfeyes set to 15.
- It was created through the attachMovie method on Frame 1, and is enthusiastically displayed in the background of the article.