Game Development Reference
In-Depth Information
regions.push(tempRegion);
}
private function setCurrentRegion(object:TileByTileBlitSprite):void {
var regionLength:int = regions.length - 1;
for (var regionCtr:int = 0; regionCtr <= regionLength; regionCtr++) {
tempRegion = regions[regionCtr];
if (object.currCol >= tempRegion.col1 &&
object.currCol <= tempRegion.col2 &&
object.currRow >= tempRegion.row1 &&
object.currRow <= tempRegion.row2) {
object.currentRegion = regionCtr;
}
}
}//** end added in iteration #5
The
setCurrentRegion
function takes a
TileByTileBlitSprite
in as a parameter called
object
.
Using the
currCol
and
currRow
attributes of the
object
, it loops through the four regions in the
regions
array to find which region the object currently is in. The object's
currentRegion
attribute
is changed to the detected region number.
Changing the readSpriteData function
We now need to modify the
readSpriteData
function to create new enemy tanks and add them to
the
enemyTanks
array. In the existing
switch:case
statement inside the
readSpriteData
function,
you will need to add a case for the enemy tanks. Add the following code below the
case
SPRITE_PLAYER
code. We have included the
SPRITE_PLAYER
case for reference. It should match
what you already have in the
GameDemoIteration5.as
file.
case SPRITE_PLAYER:
player.animationLoop = false;
playerStartRow= rowCtr;
playerStartCol= colCtr;
player.currRow = rowCtr;
player.currCol = colCtr;
player.currentDirection = MOVE_STOP;
break;
//** added in iteration #5
case SPRITE_ENEMY:
tempEnemy = new TileByTileBlitSprite(tileSheet, enemyFrames, 0);
tempEnemy.x=(colCtr * tileWidth)+(.5*tileWidth);
tempEnemy.y = (rowCtr * tileHeight) + (.5 * tileHeight);
tempEnemy.currRow = rowCtr;
tempEnemy.currCol = colCtr;
setCurrentRegion(tempEnemy);