HTML and CSS Reference
In-Depth Information
3. Next, we define the checkCollision funcion inside the Tile deiniion.
It uses the posiion coordinates to determine collision:
Tile.prototype.checkCollison = function() {
if (this.y === 400) {
if ( (this.x === game.TILE_WIDTH &&
game.player.currentLane === 1) ||
(this.x === (game.TILE_WIDTH*2) &&
game.player.currentLane === 2)) {
game.isGameOver = true;
Objective complete
We have successfully made the game over scene when a player hits an obstacle.
Determining collision
There are different approaches that we can use to determine a collision between two
game objects.
In this running game example, we use a very basic one that uses only the posiion in the
coordinate system to determine a collision.
The following figure shows the coordinates of the game. The player's y posiion is always
at 400 pixels. When the player is in the first lane, he/she has 100 pixels as the x posiion,
which is the ile's width. When the player is in the second lane, he/she has 200 pixels as
the x posiion. When the obstacle ile has the same coordinates as the player, they collide.
Search WWH ::

Custom Search