AS1 OOP: Custom Classes with MovieClips
         by senocular  

MovieClip Deconstructors
Movieclips, too, can have deconstructors defined for them. The difference with movieclips is that they aren't deleted by conventional means with the delete keyword. To dynamically delete movieclips, the removeMovieClip method must be used.

One the positive side of things, since the movieclip itself is the object, if you removed a movieclip without using a deconstructor, you probably won’t run into many problems. Though a reference to the movieclip may still exist in a listener list, because the movieclip itself is no longer present on the timeline, it can no longer contain its properties and methods etc. So, unlike other objects, event methods for removed movieclips will not continue to be called. However, the reference still exists and would be an unwanted and unneeded artifact in any listeners list. Therefore, it should be taken care of and removed.

Example:

[ deconstructor used in removing marching soldiers ]

Soldier = function(){
// add as listener of Soldier
Soldier.addListener(this);
// since this process is not argument
// dependant and only really needs to be
// done once in the process of this
// instance's life, it can be handled in
// the constructor and not init.
};
// inherit from movieclip
Soldier.prototype = new MovieClip();
// init method (constructor replacement);
Soldier.prototype.init = function(direction){
this.setDirection(direction);
this.isMarching = false;
};
Soldier.prototype.onEnterFrame = function(){
// march every frame if marching
if (this.isMarching) this.march(this.direction);
};
Soldier.prototype.march = function(direction){
// a little trigonometry to move the
// soldier based on an angle of direction
this._x += Math.cos(direction)*2;
this._y += Math.sin(direction)*2;
};
Soldier.prototype.setDirection = function(direction){
if (!this.isMarching) this.isMarching = true;
this.direction = direction;
};
// removeMovieClip over-ride/deconstructor
Soldier.prototype.removeMovieClip = function(){
trace("Removing "+this);
// remove the instance from listening to Soldier
Soldier.removeListener(this);
// call MovieClip's removeMovieClip method
// to actually remove the movieclip.
super.removeMovieClip();
};
// make soldier an event broadcaster
ASBroadcaster.initialize(Soldier);
// static method for changing soldier directions
Soldier.changeDirection = function(direction){
this.broadcastMessage("setDirection", direction);
};
// register sldr movieclip from library
// to the Soldier class
Object.registerClass("sldr",Soldier);

 
// create movieclips
for (i=0; i<10;i++){
// attach sldr clips. init object used
// to position each in a horizontal line
this.attachMovie("sldr", "s"+i, i, {_x:75+i*15, _y:150});
this["s"+i].init(Math.PI/2); // call init
};
// define button interaction
march_btn.onRelease = function(){
Soldier.changeDirection(Math.random()*2*Math.PI);
};
remove_btn.onRelease = function(){
// if soldiers remain, remove one
if (i > 0){
i--;
this._parent["s"+i].removeMovieClip();
}else{
trace("No remaining soldiers to remove.");
}
}

download source

The removeMovieClip method over-rides that which would have been otherwise inherited from MovieClip. The MovieClip removeMovieClip is still used via super so that the movieclip can still be successfully removed.

There is an alternate way of creating this relation for movieclips, and that is through altering a movieclip instance's __proto__ property.

 

Prev Page
 



SUPPORTERS:

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