Game Development Reference
In-Depth Information
Chapter 11
Organizing Game Objects
You've seen in the previous chapters how you can use classes to group variables that belong
together. This chapter looks at the similarities between the different types of game objects and how
you can express these similarities in JavaScript.
Similarities between Game Objects
If you look at the different game objects in the Painter game, you can see that they have a lot of
things in common. For example, the ball, the cannon, and the paint cans all use three sprites that
represent each of the three different colors. Also, most objects in the game have a position and a
velocity. Furthermore, all game objects need a method to draw them, some of the game objects
have a method for handling input, some of them have an update method, and so on. Now, it isn't
really a problem that these classes have similarities. The browser or the players of the game won't
complain about that. However, it's a pity that you have to copy code all the time. To give an example,
both Ball and the PaintCan class have width and height properties:
Object.defineProperty(Ball.prototype, "width",
{
get: function () {
return this.currentColor.width;
}
});
Object.defineProperty(Ball.prototype, "height",
{
get: function () {
return this.currentColor.height;
}
});
143
 
Search WWH ::




Custom Search