Game Development Reference
In-Depth Information
if (brick.freeLife) {
brick.freeLife.y += shiftHeight;
}
}
}
Immediately before creating the new rows of bricks, the function shiftBricksDown is called to clear the way for
the new level. In this function, you loop through all of the current bricks, which are stored in the bricks array. Each
brick is shifted down 80 pixels, along with any free life text object that it may be tied to. Figure 4-5 shows a new row of
bricks added to the game after reaching a new level.
Figure 4-5. A new level of bricks, pushing the first two rows down
With the initialization process complete, and the game graphics drawn to the stage, it's time to start setting the
game in motion. Let's take a look at setting up the game loop.
Setting Up the Game Loop
Understanding the process of a game loop is an important concept for any game developer. A game with constant
motion has a constant heartbeat, and its speed is determined by its frame rate. I briefly touched on this process in
Chapter 2; it's now time to see this cycle in full action.
To set up the game loop, return to the startGame function. If you haven't already, move this function to the end
of the script where you will set up the game loop. Add the function called runGame just before the stage is updated.
Listing 4-12 shows this update and the runGame function.
Listing 4-12. Revisiting the startGame Function and Running the Game
function startGame() {
createjs.Ticker.setFPS(60);
createjs.Ticker.addEventListener("tick", function (e) {
if (!e.paused) {
runGame();
stage.update();
}
});
}
 
Search WWH ::




Custom Search