Game Development Reference
In-Depth Information
The
update
method of the class contains a single instruction that updates the current position of the
game object:
ThreeColorGameObject.prototype.update = function (delta) {
this.position.addTo(this.velocity.multiply(delta));
};
Finally, you add a few convenient properties for getting and setting the color, and retrieving the
dimensions of the object. For example, this is the property for reading and writing the object's color:
Object.defineProperty(ThreeColorGameObject.prototype, "color",
{
get: function () {
if (this.currentColor === this.colorRed)
return Color.red;
else if (this.currentColor === this.colorGreen)
return Color.green;
else
return Color.blue;
},
set: function (value) {
if (value === Color.red)
this.currentColor = this.colorRed;
else if (value === Color.green)
this.currentColor = this.colorGreen;
else if (value === Color.blue)
this.currentColor = this.colorBlue;
}
});
As you can see, you use the colored sprite member variables here. Any class that inherits from
ThreeColorGameObject
also now has this property. This saves you a lot of code copying! For the
complete
ThreeColorGameObject
class, see the Painter9 example belonging to this chapter.
Cannon as a Subclass of ThreeColorGameObject
Now that you've created a very basic class for colored game objects, you can reuse this basic
behavior for the actual game objects in your game by
inheriting
from this class. Let's first look at the
Cannon
class. Because you've defined the basic
ThreeColorGameObject
class, you can create the
Cannon
class as a subclass of that class, as follows:
function Cannon() {
// to do...
}
Cannon.prototype = Object.create(ThreeColorGameObject.prototype);
You create the
Cannon.prototype
object by making a copy of the
ThreeColorGameObject.prototype
object. You still need to write the code in the constructor method, though.
Search WWH ::
Custom Search