Game Development Reference
In-Depth Information
function handleKeyDown (e) {
switch (e.keyCode) {
case ARROW_KEY_LEFT:
leftKeyDown = true;
break;
case ARROW_KEY_RIGHT:
rightKeyDown = true;
break;
}
}
function handleKeyUp(e) {
switch (e.keyCode) {
case ARROW_KEY_LEFT:
leftKeyDown = false;
break;
case ARROW_KEY_RIGHT:
rightKeyDown = false;
break;
case SPACE_KEY:
if (gameRunning) {
createjs.Ticker.setPaused(createjs.Ticker.getPaused() ? false
: true);
}
else {
resetGame();
}
break;
}
}
The setControls function adds a few keyboard listeners to the window object. These handlers are written
immediately after and handle events fired when a key is pressed and when it's released.
The rightKeyDown and leftKeyDown variables are set accordingly and used to control the paddle during the
update/render cycle that will be set up shortly. The paddle control is set up exactly how it was demonstrated in
Chapter 3. If you need to review this process, please refer to Listing 3-11.
If the key that was pressed is the spacebar, you want to do one of two things. If the game is currently in a running
state, it should pause or un-pause the game. The game can easily be paused by pausing the Ticker , which will
essentially shut down the game loop, freezing everything in its place. If the Ticker is already paused, it is un-paused to
resume the game.
If the game is not in a running state (i.e., the game over state), this action will completely reset the game by calling
a function that will also be examined later in the “Evaluating the Game” section.
At this point, the walls, message board, game puck, and paddle are finished and ready to review. Figure 4-3
demonstrates how the game looks in its current state.
Search WWH ::




Custom Search