Game Development Reference
In-Depth Information
As you can see, this class is capable of constructing the Game object that was used in the previous
chapter. The Game_Singleton class has an initialize method, where the game world object is created:
Game_Singleton.prototype.initialize = function () {
this.gameWorld = new PainterGameWorld();
};
Okay, you've discovered where the game world is constructed. But where is the instance of the
Game_Singleton object constructed? You need this instance in order to access the game world,
which will in turn give you access to the game objects. If you look at the last line of the Game.js file,
you see this instruction:
var Game = new Game_Singleton();
Finally an actual variable declaration! So through the variable Game , you can access the game world;
and through that object, you can access the game objects that are part of the game world. For
example, this is how you get to the cannon object:
Game.gameWorld.cannon
Why so complicated, you might ask? Why not simply declare each game object as a global variable
like you did before? There are several reasons. First, by declaring many global variables in different
places, your code becomes more difficult to reuse. Suppose you want to use parts of the code from
Painter in another application that also uses balls and cannons. Now you have to sift through the
code to find where global variables were declared and make sure they're useful for your application.
It's much better to declare these variables in one place (such as the PainterGameWorld class) so
these declarations are easier to find.
A second issue with using many global variables is that you're throwing away any structure or
relationship that exists between variables. In the Painter game, it's clear that the cannon and the ball
are part of the game world . Your code becomes easier to understand if you express that relationship
explicitly by letting the game objects be part of the game-world object.
In general, it's a good idea to avoid global variables as much as possible. In the Painter game, the main
global variable is Game . This variable consists of a tree structure containing the game world, which in
turn contains the game objects, which in turn contain other variables (such as a position or a sprite).
Using the new structure where the Game object is a tree structure of other objects, you can now
access the cannon object to retrieve the desired color of the ball, as follows:
if (Game.gameWorld.cannon.currentColor === sprites.cannon_red)
this.currentColor = sprites.ball_red;
else if (Game.gameWorld.cannon.currentColor === sprites.cannon_green)
this.currentColor = sprites.ball_green;
else
this.currentColor = sprites.ball_blue;
Sometimes it can be useful to sketch the tree structure of game objects on paper, or create a
diagram where you can put references with proper names later. As the games you develop become
more complex, such a tree provides a useful overview of what object belongs where, and it saves
you from having to re-create this tree mentally when working with the code.
 
Search WWH ::




Custom Search