Game Development Reference
In-Depth Information
Figure 14-1.
A screenshot of the JewelJam2 example program
Grid Operations
Because you've organized part of the game world in a grid, you can now use
for
loops in a smart
way to add
behavior
to the grid. In this example, you add a feature that moves each row down one row.
This means the last row disappears and you need to generate new (random) values for the first row.
Let's add a method called
moveRowDown
that does this. What does it mean to “move down” a row?
Basically, you simply have to copy the values in the row at index
y
to the row at index
y + 1
. Let's
put this in a
for
loop:
for (var y = 1; y < this.rows - 1; y++) {
for (var x = 0; x < this.columns; x++) {
this.setGridValue(x, y + 1, this.getGridValue(x, y));
}
}
Search WWH ::
Custom Search