Game Development Reference
In-Depth Information
to the world tile. The
numberToPush
variable holds the number to put into the
world
array for the current row and column. Most of the time, this number will be the same as
tileNum
, with the exception of the player car tile. The player car tile will be replaced
with the
levelBackGroundTile
.
2.
We use the
levelData = levels[level]
setting to create a reference to the current
Level
data.
3.
We assign values from the current level to our level-specific variables (discussed
earlier in the section on the Level class).
4.
Next, we loop through all of the rows nodes in the
levelData.map
2D array for the level
and create another nested for loop to iterate through each column inside the row. We
create a temp array (
tempArray
) to hold all of the column data. It will be pushed into
the
world
array on completion of each row loop iteration.
5.
If the tile is a
TILE_WALL
or
TILE_MOVE
tile, we simply push the
tileNum
into the
tempArray
. If it is not, we drop down into the
switch:case
for sprites.
6.
Unlike No Tanks! where all of the game objects were individual AS3 Sprites, only the
player will be a separate display object. All of the other sprites will simply be tiles in the
world. We will use the
LookAheadPoint
instances to do tile-based collision detection
between these sprite tiles and the car rather that the
BitmapData
collision detection we
used in the No Tanks! games.
7.
The
switch:case
statement is used to determine the tile type and set up variables
associated with each type.
8.
At the end of the inner loop, the
numberToPush
variable is inserted into the
tempArray
.
9.
At the end of the outer loop, the
tempArray
is pushed into the
world
array creating a
2D array of 50
50 tiles for our world.
10.
Finally, we multiply the
heartsTotal
value by the
levelPrecentNeeded
to get the
heartsNeeded
for the level.
The restartPlayer function
The
restartPlayer
function contains quite a bit of logic associated with positioning the player car
on the screen in the correct location. Player positioning is a very important topic that we will
explore in some detail after you look at the code. The player object needs to be positioned to the
viewable screen (384
384) but also in relation to full world. When the player object moves on
the screen, the player does not move; the camera does instead. This forces the player to stay in
the middle of the screen, which cannot happen properly if the player drives near the edge of the
world. We compensate for this limitation by allowing the player move freely when near the edge
of the world; in turn, the camera does not move.
You will see this logic when we explore the
update
function. For the
restartPlayer
function, we
simply need to realize that the same rules apply. Since, most of the time, the player will probably
start a level near the edge of the screen, at least one direction (vertical or horizontal or both) will
need to offset from an edge of the screen, pushing or pulling the player away from the center. If
we didn't do this, the player would not show up in the correct starting tile.