Table of ContentsCollision DetectionConclusion

Game Over

If a collision is detected, you need to trigger an end to the current game. To do this, the collision-checking code changes the running Boolean to false. If you remember the thread code, you know that this will cause the main game thread to fall through its while loop. Now modify it to do something when the thread ends.

public void run()
{
   while(running)
   {
      ...
   }

   // fall back to the splash form at the end of our loop
   theMidlet.gameOver();
}

Finally you can add the gameOver method to the application class. This will display an alert message and then change the display back to the menu.

/**
 * Called by the game classes to indicate to the user that the current game
 * is over.
 */
public void gameOver()
{
   // Display an alert (the device will take care of implementing how to
   // show it to the user.
   Alert alert = new Alert("", "Splat! Game Over", null, AlertType.INFO);
   // Then set the menu to be current.
   Display.getDisplay(this).setCurrent(alert, menu);
}

And without further ado, your masterpiece is complete. Congratulations, you've now covered all the elements required to make a fully functioning game!

If you want to push on with the game yourself, I recommend adding some simple scoring. For example, you could keep track of the number of times the wombat crosses the road before a timer counts down, and then award points for each crossing. The more times you cross, the higher the points awarded. When the time is up, move the player to a new level, increasing the speed of the vehicles and the value of the points along the way. I also recommend making death a little less permanentmaybe just use the time penalty of having to restart.

    Table of ContentsCollision DetectionConclusion