Game Development Reference
In-Depth Information
the sprite-loading code, you simply have to include the source file containing the code into your new
game application, instead of sorting through the Painter game code to find what you need. Another
reason to do this is to enable you to more quickly create similar games later. You might find yourself
developing a game that is similar to a game you developed earlier. By organizing code in this way,
you can produce your new game more rapidly while also not reinventing the wheel.
The Painter4 example creates a separate
Game.js
file that contains the
Game
object and a number
of useful methods belonging to that object. The parts that are specific to Painter have been moved
to the
Painter.js
file. In that file, you have a method for loading sprites as well as a method for
initializing the game. Furthermore, a new file called
PainterGameWorld.js
handles the various objects
in the game. In the previous versions of Painter, this game world consisted only of a background
image and a cannon. In the next section, you add a
ball
to this game world. The Painter game world
is then defined by an object that makes sure all the game objects are updated and drawn. This is
part of the definition of the
painterGameWorld
object:
var painterGameWorld = {
};
painterGameWorld.handleInput = function (delta) {
ball.handleInput(delta);
cannon.handleInput(delta);
};
painterGameWorld.update = function (delta) {
ball.update(delta);
cannon.update(delta);
};
painterGameWorld.draw = function () {
Canvas2D.drawImage(sprites.background, { x : 0, y : 0 }, 0,
{ x : 0, y : 0 });
ball.draw();
cannon.draw();
};
When you initialize the game, you initialize the game objects, and you tell the
Game
object that the
object governing the game world is
painterGameWorld
, as follows:
Game.initialize = function () {
cannon.initialize();
ball.initialize();
Game.gameWorld = painterGameWorld;
};
Inside the
Game.mainLoop
method, you now only have to make sure to call the right methods on the
gameWorld
variable (which refers to
painterGameWorld
):
Game.mainLoop = function () {
Game.gameWorld.handleInput();
Game.gameWorld.update();
Canvas2D.clear();
Search WWH ::
Custom Search