Game Development Reference
In-Depth Information
The code is exactly the same, but you have to copy it for both classes. And every time you want to
add a different kind of game object, you probably need to copy these properties again. In this case,
these properties are fortunately not that complicated, but in the application you're copying around
a lot of other things as well. For example, most game object classes in the Painter game define the
following member variables:
this.currentColor = some sprite ;
this.velocity = Vector2.zero;
this.position = Vector2.zero;
this.origin = Vector2.zero;
this.rotation = 0;
The draw methods of the various game objects also look similar. For example, here are the draw
methods of the Ball and the PaintCan classes:
Ball.prototype.draw = function () {
if (!this.shooting)
return;
Canvas2D.drawImage(this.currentColor, this.position, this.rotation, 1,
this.origin);
};
PaintCan.prototype.draw = function () {
Canvas2D.drawImage(this.currentColor, this.position, this.rotation, 1,
this.origin);
};
Again, the code is very similar in the different classes, and you copy it every time you make a new
kind of game object. In general, it's better to avoid copying a lot of code. Why is that? Because
if at some point you realize there is a mistake in that part of the code, you have to correct it
everywhere you copied it to. In a small game like Painter, this isn't a big issue. But when you
develop a commercial game with hundreds of different game-object classes, this becomes a serious
maintenance problem. Furthermore, you don't always know how far a small game will go. If you
aren't careful, you can end up copying a lot of code (and the bugs associated with it). As a game
matures, it's a good idea to keep an eye out for where to optimize code, even if this means some
extra work to find these duplications and consolidate them. For this particular situation, you need
to think about how the different kinds of game objects are similar and whether you can group these
similarities together, just as you grouped the member variables in the previous chapters.
Conceptually speaking, it's easy to say what is similar between balls, paint cans, and cannons:
they're all game objects . Basically, they can all be drawn at a certain position; they all have a velocity
(even the cannon, but its velocity is zero); and they all have a color that is red, green, or blue.
Furthermore, most of them handle input of some kind and are updated.
 
Search WWH ::




Custom Search