Game Development Reference
In-Depth Information
This line needs to exist in this exact spot. The player cannot be instantiated until the
playerFrames
is populated with the list of frames needed to animate the player. If you recall from
Chapter 6, this is assigned in the
initTileSheet
function. Also, the
readSpriteData
function
cannot execute unless the player has been created. A runtime error will occur if the player sprite
is not created before the
readSpriteData
function executes, because the game engine needs to
place the player on the screen and cannot do so unless it has been created.
The iteration 1 restartPlayer function
This function is used to restart the player for a new level or after death. We don't need the local
afterDeath
variable yet, but it will be necessary later because when the player actually dies, the
new tank that replaces the old one will be given maximum
healthPoints
and some starting
ammunition. When a player starts a new level without dying, these attributes are not affected. So,
to use the same function for both occurrences, the
afterDeath
Boolean is passed in.
These six lines are the most important to discuss in setting up the player for a player for restart:
player.currCol = playerStartCol;
player.currRow = playerStartRow;
player.x=(playerStartCol * tileWidth)+(.5*tileWidth);
player.y = (playerStartRow * tileHeight) + (.5 * tileHeight);
player.nextX = player.x;
player.nextY = player.y;
The first thing we do here is to calculate the current row and column on the game grid that the
player resides in. We can do this at the beginning of a level (or on restart after death) because we
know the player is in the center of the current tile.
Next, we set the
x
and
y
coordinates for the player using the
player.currCol
and
player.currRow
variables we just set. Here, we find the
x
value of the top-left corner of the tile that the player
needs to be in;
playerStartCol * tileWidth
is this location. We do a similar operation for the
top-left corner of the
y
position. The player tank will not sit in this exact position though. Notice
that the player's position is set an additional 0.5 multiplied by the
tileWidth
for
x
and 0.5 times
the
tileHeight
for
y
. This offset is needed because the
Bitmap
inside the
BlitSprite
that
represents the player has been moved to -0.5 multiplied by the width and -0.5 multiplied by the
height center it on the registration point of the
Sprite
. If we don't reposition it further to the right
and down, it would be 16 pixels off in left and down directions.
The
nextX
and
nextY
variables are set to be equal to the current
x
any
y
values for the player. These
are important because they will be used in the
update
portion of the game loop. We will update the
nextX
and
nextY
values, then do collision detection based on them (as well as grid based logic
movement evaluation), and finally set
x
and
y
to equal
nextX
and
nextY
for the
render
portion of the
game loop. We will examine these in detail in a later iteration through the game code.
The readSpriteData function
The
readSpriteData
function was merely an empty stub in
GameDemo.as
. We are now going to
fill it out with the necessary code to loop through the sprite data and place the player on the
game screen:
private function readSpriteData():void {
var tileNum:int;
var spriteMap:Array = levelData.spriteMap;