Table of ContentsThe Application ClassThe Game Screen

The Menu

To start the game, present the player with a splash screen and menu. This is just a simple form with text introducing the title of the game and offering an option to either start playing or quit. This screen also serves as an anchor point to return to when play has ended. You'll see this in action later. For now, here's the menu class:

import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable; 
/**
 * A simple menu system for RoadRun using commmands.
 * @author Martin J. Wells
 */
public class MainMenu extends Form implements CommandListener
{
   private RoadRun theMidlet;
   private Command start;
   private Command exit;

   /**
    * Creates a new menu object (while retaining a reference to its parent
    * midlet).
    * @param midlet The MIDlet this menu belongs to (used to later call back
    * to activate the game screen as well as exit).
    */
   protected MainMenu(RoadRun midlet)
   {
      // the Form constructor
      super("");

      // a connection to the midlet
      theMidlet = midlet;

      // add the welcome text
      append("\nWelcome to\n");
      append("R-O-A-D R-U-N");

      // create and add the commands
      int priority = 1;
      start = new Command("Start", Command.SCREEN, priority++);
      exit = new Command("Exit", Command.EXIT, priority++);

      addCommand(start);
      addCommand(exit);

      setCommandListener(this);
   }

   /**
    * Handles the start (activate game screen) and exit commands. All the work  
    * is done by the RoadRun class.
    * @param command the command that was triggered
    * @param displayable the displayable on which the event occurred
    */
   public void commandAction(Command command, Displayable displayable)
   {
      if (command == start)
         theMidlet.activateGameScreen();
      if (command == exit)
         theMidlet.close();
   }
}

This class extends javax.microedition.lcdui.Form to provide a simple placeholder for the intro text (your splash screen). The constructor then adds this text along with the start and exit commands.

The commandAction method handles the two commands, both of which use the theMidlet field to make a callback to the application class. exit simply calls the close method on the MIDlet. start, however, calls a method you haven't implemented yet, activateGameScreen. Of course, before you can activate it, you need to know what exactly a GameScreen is.

    Table of ContentsThe Application ClassThe Game Screen