Game Development Reference
In-Depth Information
A few more things are needed to complete the function. Earlier, a random number was set to determine if the
current iteration should draw a bonus brick. Along with coloring the brick green, a simple text object will be added to
help indicate what the prize will be when busting it. A white text object with the value of '1UP' is centered above the
brick. A reference to this text object is assigned to the brick so it can be properly disposed of when the brick is hit. The
text object is then added to the stage.
The last piece of code in this loop is a simple conditional to check if a new row should start in the two-row level.
And finally, to end the function, the level is increased. There are only so many levels in the levels array, so you
continue to use the last data object for the remainder of the game by decreasing the level by one. Figure 4-4 shows the
first two levels of bricks added to the game.
Figure 4-4. The game and bonus bricks for the first two levels
Shifting the Bricks
Shifting all existing bricks makes room for the next level. This is done before adding the new level, and is shown in
Listing 4-11.
Listing 4-11. The shiftBricksDown Function Moves all Bricks Down when Adding a New Level
function shiftBricksDown() {
var i, brick;
var shiftHeight = 80;
var len = bricks.length;
for (i = 0; i < len; i++) {
brick = bricks[i];
brick.y += shiftHeight;
 
Search WWH ::




Custom Search