Game Development Reference
In-Depth Information
Orb.prototype.Shape_initialize = Orb.prototype.initialize;
The orb's
initialize
function is then created. At the very least, this function needs to fire its super class's
initialize
function, which is absolutely essential in order to fully extend all of the behavior of the class you are
extending. In other languages, this is equivalent to calling a
super
method inside a constructor function.
this.Shape_initialize();
Finally, the
Orb
class gets added to
window
so it is accessible throughout your application. Again, at this point this
Orb
is nothing more than a
Shape
object. To make it unique, some custom properties and methods need to be added
to it. This was already done with the
initialize
function. This is done by using
prototype
.
Orb.prototype.initialize = function(){
};
You can similarly add properties to your custom class.
Orb.prototype.points = 4;
Let's put this all together to see the
Orb
class in completion, with the option to pass in a different color to each orb
(see Listing 8-2).
Listing 8-2.
Sample Orb Class that Extends Shape
(function() {
var Orb = function(color) {
this.initialize(color);
}
Orb.prototype = new createjs.Shape();
Orb.prototype.Shape_initialize = Orb.prototype.initialize;
Orb.prototype.points = 4;
Orb.prototype.initialize = function(color) {
this.Shape_initialize();
this.graphics.beginFill(color).drawCircle(0,0,20);
}
Orb.prototype.die = function(){
createjs.Tween.get(this).to({alpha:0},100).call(function(e){
this.parent.removeChild(this);
});
}
window.Orb = Orb;
}());