Game Development Reference
In-Depth Information
Chapter 8
Game Object Types
In the previous chapters, you've seen how to create a game world that contains a few different
game objects, such as a cannon and a ball. You've seen how to let game objects interact with each
other. For example, the ball object updates its color according to the color of the cannon. In this
chapter, you add falling paint cans to the game world. However, before you can do that, you have to
reexamine how to create and manage objects in JavaScript. I introduce the class concept as a means
to create multiple game objects of a certain type. Then, you apply the class concept to other parts of
the Painter game application. Furthermore, you learn how to incorporate randomness in your games.
Creating Multiple Objects of the Same Type
Until now, you only needed one instance of each game object in Painter. There is only one cannon
and one ball. The same holds for all the other objects in the JavaScript code. There is a single Game
object, a single Keyboard object, a single Mouse object, and so on. You create these objects by
declaring a variable referring to an empty or a composite object and adding useful methods to it.
For example, here is how you create the ball object:
var ball = {
};
ball.initialize = function() {
ball.position = { x : 0, y : 0 };
// etc.
};
ball.handleInput = function (delta) {
if (Mouse.leftPressed && !ball.shooting) {
// do something
}
};
// etc.
103
 
Search WWH ::




Custom Search