Game Development Reference
In-Depth Information
Detecting collisions and the end of a level or the game
(iteration 5)
In this iteration, we will build out most of the rest of our game logic. At the end of iteration 4, we
had completed the update and render functions for our game. The car can now drive around the
scrolling game world but will not be affected by the walls, hearts, or other sprites.
The collision detection routine for Drive She Said will use the three LookAheadPoint objects we
created earlier. They are positioned at points that represent the next location for our player car.
We have three of them to insure proper coverage across the surface of the car that will most
likely hit an object. We will check the tile type that each of the three will be located in based on
the player.nextX and player.nextY values that are updated in the update function. We also set
the player.move variable to true in the update function. During the collision detection, if any of the
three points hits a wall, this will be set to false .
If we had handled collisions another way, such as setting player.move to false first and then
checking collisions and setting it to true if any of the LookAheadPoints was not on a wall, we
would get strange behavior if one point was on the wall and one was not.
Let's take a look at the code for this now.
The checkCollisions function
This is the code for the checkCollisions function:
private function checkCollisions():void {
var lookAheadLength:int = lookAheadPoints.length;
var row:int;
var col:int;
var tileType:int;
//loop through all three look ahead points
for (var ctr:int = 0; ctr < lookAheadLength; ctr++) {
row = (lookAheadPoints[ctr].y+camera.y) / mapTileHeight;
col = (lookAheadPoints[ctr].x + camera.x) / mapTileWidth;
tileType = tileSheetData[world[row][col]];
switch(tileType) {
case TILE_MOVE:
//do not need to do anything
break;
case TILE_WALL:
if (canvasBitmapData.getPixel(lookAheadPoints[ctr].x,
lookAheadPoints[ctr].y) != levelWallDriveColor) {
if (Math.abs(player.velocity) > 1) {
//don't keep on playing sound if close to a wall
Search WWH ::




Custom Search