Game Development Reference
In-Depth Information
belonging to the first button that was pressed. If the player didn't press any button, the method
returns - 1. Here is the complete method:
LevelMenuState.prototype.getSelectedLevel = function () {
for (var i = 0, j = this.levelButtons.length; i < j; i += 1) {
if (this.levelButtons[i].pressed)
return this.levelButtons[i].levelIndex;
}
return -1;
};
You can then use this method in the handleInput method:
LevelMenuState.prototype.handleInput = function (delta) {
GameObjectList.prototype.handleInput.call(this, delta);
var selectedLevel = this.getSelectedLevel();
if (selectedLevel != -1) {
// start playing the level
}
else if (this.back.pressed)
GameStateManager.switchTo(ID.game_state_title);
};
As you can see, adding different states to a game and switching between them isn't very hard, as
long as you think about the design of the software beforehand. By thinking in advance about which
classes are needed and how the functionality of your game should be split up between them, you
can save yourself a lot of time later. In the next chapter, you further extend this example by creating
the actual levels. Figure 20-1 shows a screenshot of the level menu state.
 
Search WWH ::




Custom Search