AS2 OOP: Inheritance
         by senocular

MovieClips
Flash can be a little unorthodox in the programming world because of these oddities it uses called MovieClips. MovieClips are their own objects - Flash objects, not yours. This can make them a little difficult to work with since you have to pull some trickery in order for them to behave like your objects.

With ActionScript 1.0, Object.registerClass is used to "register" a movieclip to a specific class (which was of course made a subclass of the MovieClip object). What this did was tricked the MovieClip being created into thinking it wasn't a basic MovieClip object, but more specifically the object set up in the registration (though at heart, it would still be a MovieClip).

This was all well and good but it wasn't without its issues. Those issues, for the most part, sadly still exist with ActionScript 2.0. However, the use of Object.registerClass itself has been simplified and made a little convenient as it was added as option in a MovieClip symbols linkage properties.

[ linkage properties dialog allows movieclip class association ]

This takes away the necessity to use Object.registerClass at all. That is of course unless you need to change that association at run-time (which very well may be the case). Remember that classes linked this way still need to inherit from MovieClip.

Example: Bouncing Balls

[ ball movieclips as bouncer class instances ]

// in Bouncer.as
class Bouncer extends MovieClip {
// variables, all private - internally handled
private var gravity:Number = 0;
private var boundaries:Object;
private var velocity_x:Number;
private var velocity_y:Number;
private var original_x:Number;
private var original_y:Number;
 
// constructor
function Bouncer() {
original_x = _x;
original_y = _y;
Mouse.addListener(this);
onMouseDown(); // force mouseDown event
}
 
// init method for arguments which in other normal
// circumstances (non-MovieClip classes) would be
// given to the constructor. Not an option with MovieClips
function init (pGravity:Number, pboundaries:Object) {
gravity = pGravity;
boundaries = pboundaries;
return this;
}
// onEnterFrame event to control bouncing
private function onEnterFrame():Void {
// move ball
_x += velocity_x;
_y += velocity_y;
 
// check for boundry collision
if (_x <= boundaries.left) {
_x = boundaries.left;
velocity_x = Math.abs(velocity_x);
}else if (_x >= boundaries.right) {
_x = boundaries.right;
velocity_x = -Math.abs(velocity_x);
}
if (_y <= boundaries.top) {
_y = boundaries.top;
velocity_y = Math.abs(velocity_y);
}else if (_y >= boundaries.bottom) {
_y = boundaries.bottom;
velocity_y = -Math.abs(velocity_y);
}
 
// apply gravity
velocity_y += gravity;
}
 
// reset original position and apply a new
// random direction when the mouse is pressed
private function onMouseDown():Void {
_x = original_x;
_y = original_y;
velocity_x = Math.random()*20-10;
velocity_y = -Math.random()*20;
}
}
// in Flash Movie
var balls_bounds:Object = {left:10, top:10, right:290, bottom:290};
for (var i = 0; i < 5; i++){
attachMovie("Ball", "ball"+i, i, {_x:150, _y:150}).init( 5, balls_bounds );
}

 

download full source

 

 




SUPPORTERS:

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