Game Development Reference
In-Depth Information
The IGameLoopObject interface forms the basis of all objects that have game-loop methods. You can
change the existing classes in the example to follow this approach. For example, the GameObject
class now also inherits from IGameLoopObject :
function GameObject(layer, id) {
IGameLoopObject.call(this);
// initialize the game object...
}
GameObject.prototype = Object.create(IGameLoopObject.prototype);
Have a look at the classes in the PenguinPairs4 example to see how the IGameLoopObject class in
integrated into the program design. As you can see, the example adds a PlayingState class that
also inherits from IGameLoopObject :
function PlayingState() {
IGameLoopObject.call(this);
// initialize the playing state...
}
PlayingState.prototype = Object.create(IGameLoopObject.prototype);
Creating the Levels in the Playing State
In this section, you create the levels in the game from the data stored in the global windows.LEVELS
variable. To represent a level, you create a Level class that inherits from GameObjectList . For each
level that needs to be created, you create a Level instance and fill it according to the data in the
global LEVELS variable. In the PlayingState constructor, you initialize an array in which you store all
these instances. You also store the level the player is currently playing:
this.currentLevelIndex = -1;
this.levels = [];
Then you call a method loadLevels , which is responsible for creating the Level instances from the
level data:
this.loadLevels();
In the loadLevels method, you place a for loop in which you create Level instances. In the Level
constructor, you translate the level data into actual game objects that are part of each level:
PlayingState.prototype.loadLevels = function () {
for (var currLevel = 0; currLevel < window.LEVELS.length; currLevel++)
this.levels.push(new Level(currLevel));
};
 
Search WWH ::




Custom Search