AS1 OOP: Custom Classes with MovieClips
by senocular
Constructor VS Init Object
As you probably know, attachMovie allows you to pass an
optional init object in its call. Any properties of this
object are then copied into the attached movieclip instance.
This can save time and space when adding multiple properties
to many consecutively attached movieclips. Here’s a quick
example.
- // declare init object
- defaultValues = {prop: "value", _x: 100 };
- // add an 'id' movieclip named movie; it will have
- // a prop = "value" and an _x position of 100
- this.attachMovie("id", "movie", 1, defaultValues);
- trace(movie.prop); // traces "value"
- trace(movie._x); // traces 100
This init object acts much like a constructor function
might, defining initial values for an instance when its
created. With an init object, however, you don’t have much
control over those properties and have no means for running
methods or other functions/expressions as you would in a
constructor. Whatever values are included in the init
object, are those given directly to the movieclip without
any other direction.
Class constructors give you more control with your data
in object creation, but with movieclips and registered
classes, you run into a problem. That problem is that there
is no means of passing arguments to a class constructor when
its run for a movieclip. For other non-movieclip objects,
values are passed through the constructor call in creating
the object instance. With movieclips however, since they are
created in the timeline or through attachMovie, there is no
place to pass values to serve as arguments of your
movieclip’s class constructor. About the closest thing you
have is the init object (which obviously is only available
when using attachMovie and not relying on the timeline).
The init object of attachMovie has its properties copied
to the movieclip before its constructor is run. Because of
this, you can essentially use it to pass parameters of sorts
to your constructor only they aren’t necessarily passed.
They’re more pre-defined in the movieclip itself than passed
in the local scope of the constructor function. That being
the case, you may need remove un-necessary values passed in
this manner. Here’s an example where that may be the case.
- Helper = function(){
- if (this.status == "inactive"){
- this._alpha = 50;
- }
- delete this.status;
- };
- Helper.prototype = new MovieClip();
- Object.registerClass("helperClip", Helper);
- screenStatus = "inactive";
- this.attachMovie("helperClip", "helper1", 1, { status:
screenStatus } );
- trace(helper1._alpha); // traces 50
- trace(helper1.status); // traces undefined
Helper instances here are movieclips who have a
transparency based on a certain ‘screenStatus’ when created.
For the constructor to receive this value, it was passed as
a value in an init object (defined directly in the
attachMovie call). As an init object though, that status
variable is defined directly in the attached Helper
movieclip – something you may not want in that movieclip. If
its not wanted, it will need to be removed, as is the case
in this example where status is deleted at the end of the
constructor call.