Table of ContentsGame DesignThe Menu

The Application Class

Given the (albeit simple) game design, you can move on to the classes you'll need to get the job done. The first and most obvious is a MIDlet class to represent the application. I prefer the convention of using the application name without tacking MIDlet on the end for this class, so call your first class RoadRun.

The role of this class, like any good MIDlet, will be to act as your connection to the Application Manager (it will call the MIDlet startApp, pauseApp and destroyApp methods to let you know when the game is starting, pausing, or about to be destroyed). Here's the complete class:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * The application class for the game RoadRun.
 * @author Martin J. Wells
 */ 
public class RoadRun extends MIDlet
{
   private MainMenu menu;

   /**
    * MIDlet constructor instantiates the main menu.
    */
   public RoadRun()
   {
      menu = new MainMenu(this);
   }

   /**
    * 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);
   }

   /**
    * A utility method to shutdown the application.
    */
   public void close()
   {
      try
      {
         destroyApp(true);
         notifyDestroyed();
      }

      catch (MIDletStateChangeException e)
      { }
   }

   /**
    * Handles Application Manager notification the MIDlet is starting (or 
    * resuming from a pause). In this case we set the menu as the current
    * display screen.
    * @throws MIDletStateChangeException
    */
   public void startApp() throws MIDletStateChangeException
   {
      Display.getDisplay(this).setCurrent(menu);
   }

   /**
    * Handles Application Manager notification the MIDlet is about to be paused.
    * We don't bother doing anything for this case.
    */
   public void pauseApp()
   {
   }

   /**
    * Handles Application Manager notification the MIDlet is about to be
    * destroyed. We don't bother doing anything for this case.
    */
   public void destroyApp(boolean unconditional) throws MIDletStateChangeException
   {
   }

}

As you can see, this is the most basic of MIDlets. You just implement the required methods, instantiate your menu, and make it the current display. This code won't work as it is though; you need to implement the menu system (the MainMenu class).

    Table of ContentsGame DesignThe Menu