Game Development Reference
In-Depth Information
dispatchEvent(new CustomEventSound(CustomEventSound.PLAY_SOUND,
Main.SOUND_LEVEL_COMPLETE, false, 1, 0));
break;
}
}
}
}
For the collision detection we are going to loop through each of the three LookAheadPoint
references and check the tile type that each is colliding with.
1.
If the tile is the TILE_MOVE type, we do nothing. This part of the switch:case statement
added just for reference.
2.
If the tile is the TILE_WALL type, we do a second check to see if the pixel located at the
same place as the point is a color other than the levelWallDriveColor . If it is, we
assume that it is part of the wall and proceed with the collision reaction code:
If the velocity of the player is greater than 1, we will play the sound for hitting the wall.
We only want to do this one time per hit, so the code that follows will also make sure
the velocity is less than 1 on the next frame tick. We use Math.abs so this will work in
both for forward and reverse directions.
In the event that the car overshoots the wall and is stuck and moving too slow to be
bounced back, we make sure that the player's car is at least moving with velocity of 1
in its current direction. This will force the next lines of code to bump the player away
from the wall.
To ensure that the velocity for the player will be less than 1 on the next frame tick, we
add the inverse of the levelWallAdjust to player.velocity , player.dx , and player.dy
to effectively move the player car back in the opposite direction it was moving then it
hit the wall.
We make sure the player.move variable is set to false for this frame tick. On the next
tick, the player will move back.
3.
If the player hits a SKULL sprite we add the inverse of the levelSkullAdjust variable to
the player.dx , player.dy , and player.velocity in a similar manner as the wall hit. We
also switch the tile out with the background tile so it will not be on the screen anymore
and play the associated sound for the skull hit.
4. If the player hits a CLOCK sprite, we add seconds to the countDownTimer :
countdownTimer.seconds += levelClockAdd . We also switch the tile out with the
background tile so it will not be on the screen anymore and play the associated
sound for the clock hit.
5.
If the player hits a HEART sprite, we add seconds to add to the heartsCollected
variable and to the score. We also switch the tile out with the background tile so it will
not be on the screen anymore and play the associated sound for the heart hit.
Search WWH ::




Custom Search