HTML and CSS Reference
In-Depth Information
for example, don't want to collide with themselves, but they do want to collide with the player and the play-
er's missiles. By doing a bitwise AND operation, collisions against objects of multiple types can be performed
without the loss of speed that an array or hash lookup would require. One caveat is that each of the different
types must be a power of two to prevent overlap of different types.
For example, if types were defined as the following:
an enemy could check if it collides with a player or a player's missile by doing a bitwise OR of the two types
Objects can also be assigned multiple types, and the collide function would still work as planned.
With that, the GameBoard class is complete. See gameboard/engine.js for the full version of the ob-
ject in the code for this chapter.
Adding GameBoard into the Game
With the GameBoard class complete, the next step is to add it into the game. A quick modification of the
playGame function from game.js does the trick:
var playGame = function() {
var board = new GameBoard();
board.add(new PlayerShip());
Reload the index.html file, and you should see exactly the same behavior as at the end of Chapter 1. All
that's been done is to have the GameBoard take over managing the ship sprite. This is less than impressive
because so far the game isn't putting the GameBoard class to good use because it just has a single sprite in it.
This is remedied in the next section.
Firing Missiles
Now it's time to give the player something to do besides just fly left and right across the screen. You are going
to bind the spacebar to fire off a pair of projectiles.
Adding a Bullet Sprite
The first step to giving the player some destructive capacity is to create a blueprint for the player missile object.
This object is added to the game at the player's location whenever the player presses the fire key.
The PlayerShip object didn't use the object prototype to create methods because in general there is only
one player in the game at a time so it's unnecessary to optimize for object creation speed or memory footprint.
Search WWH ::

Custom Search