Game Development Reference
In-Depth Information
Chapter 9
Colors and Collisions
By now, you've implemented quite a large part of the Painter game. You've seen how to define game
object classes by using the prototype mechanism. By using these classes, you gain more control
over how game objects are structured and how you can create game objects of a certain type. You
separate these class definitions over different files. That way, when you need a cannon or a ball with
the same behavior in a future game you're working on, you can simply copy these files and create
instances of these game objects in your game.
When you look more closely at the definition of a class, you can see that a class defines the internal
structure of an object (which variables it consists of) as well as methods that manipulate this object
in some way. These methods can help define more precisely what the possibilities and limitations
of an object are. For example, if someone wanted to reuse the Ball class, they wouldn't need a lot
of detailed information about how a ball is structured. Simply creating an instance and calling the
game-loop methods suffices to add a flying ball to the game. Generally, when you design a program,
whether it's a game or a completely different kind of application, it's important to clearly define what
is possible with objects of a certain class. Methods are one way to do that. This chapter shows you
another way to define an object's possibilities: by defining properties . The chapter also introduces a
type for representing colors and shows how to handle a collision between the ball and a paint can
(if that happens, the paint can needs to change color).
A Different Way to Represent Colors
In the previous versions of Painter, you've dealt with colors rather practically. For example, in the
Cannon class, you kept track of the current color by using the currentColor variable, which initially
points to the red cannon sprite:
this.currentColor = sprites.cannon_red;
121
 
Search WWH ::




Custom Search