HTML and CSS Reference
In-Depth Information
The Enemy
Each enemy objecthasitsownsetofattributesthatareverysimilartothoseoftheplayer.Like
the player, each enemy is an object instance.
Here is the code from the createPlayField() function that sets up the attributes for a new
enemy object:
EnemyLocationFound = true
true ;
var
var tempEnemy = {};
tempEnemy . row = randRow ;
tempEnemy . col = randCol ;
tempEnemy . nextRow = 0 ;
tempEnemy . nextCol = 0 ;
tempEnemy . currentTile = 0 ;
tempEnemy . rotation = 0 ;
tempEnemy . x = tempEnemy . col * 32 ;
tempEnemy . y = tempEnemy . row * 32 ;
tempEnemy . speed = 2 ;
tempEnemy . destinationX = 0 ;
tempEnemy . destinationY = 0 ;
tempEnemy . dx = 0 ;
tempEnemy . dy = 0 ;
tempEnemy . hit = false
false ;
tempEnemy . dead = false
false ;
tempEnemy . moveComplete = false
false ;
enemy . push ( tempEnemy );
items [ randRow ][ randCol ] = 1 ;
A few extra things are worth pointing out in this code. The first is that each enemy object
needs a moveComplete attribute. This is used in the animateEnemy() game state function.
When the entire enemy battalion has moved to its new location, the game transitions to the
next game state. This is discussed in detail in the next section, Turn-Based Game Flow and
the State Machine .
Notice, too, that the new enemy objects are added to the enemy array and to the items mul-
tidimensional array. This ensures that the player and the goal cannot be placed on an enemy
location. After the enemy moves from its initial location, the playField array still has a road
tile to show in its place. We call the player and the enemy moving-object tiles because they
can move about the game board. When they move, they must uncover the road tile in the spot
they were in before moving.
Now let's take a quick look at the goal tile to solidify your understanding of the difference
between the playField and the moving object tiles.
Search WWH ::




Custom Search