Game Development Reference
In-Depth Information
between their two tile locations. We then use a nested
if:else
structure to find which
direction we need to test. The local
testDX
and
testDY
variables represent the number
of tiles in each direction to move during each step of the test. The
testRotation
is the
rotation the predator will need to assume to fire at the player if the test comes out true.
5.
If the current column for
predator
and
prey
are not the same, next we check the rows
in the same manner as in step 3.
6.
Finally, if they are not in the same row or column, we set the
difference
to
0
. This will
prevent the
for
loop in the next section from firing, and we will end this
predator
's line
of sight test.
7.
We loop from
0
to the
difference
(difference between the
predator
and
prey
tile
locations was calculated in step 4). On each loop iteration, we add the
testDX
value to
the
checkCol
and the
testDY
to the
checkRow
. If the enemy hits the player's current tile
without stopping at a wall or going off the screen, it fires at the player (and turns in the
direction of the player to do so).
The fireMissileAtPlayer stub function
We add the
fireMissileAtPlayer
function now as a placeholder until we add projectiles in the next
section. It will contain a trace so we can see the enemy tank AI logic firing missiles at the player.
private function fireMissileAtPlayer(enemySprite:TileByTileBlitSprite):void {
trace("fire at player");
}
Adding to the variable definitions
We still have not added the
moveDirections
array that was used in the
chaseObject
function to the
variable definition section for this iteration. Add the following line to the variable definition section:
private var moveDirectionsToTest:Array;
Testing iteration 6
When you test this iteration, you should see all of the enemy tanks placed on the screen (see
Figure 7-7). They should move toward the player if they are in the same region and also turn and
fire (in a trace for now). You will notice that this will fire off
a lot
of trace statements. If this slows
down your IDE or Flash Develop, be sure to remove some and test it again.