This class contains a list of commands to be accessed by the player.
Initially it contains New Game
and Graphics3D
properties,
but when the game starts, a new command is added to switch between normal
and top views.
Create the MenuList
class
file.
Import the required classes.
import javax.microedition.lcdui.*; import javax.microedition.m3g.*;
Write the
below code to create the MenuList
class, set it to extend List
and
to implement CommandListener
.
class MenuList extends List implements CommandListener { private final Maze3DMIDlet midlet; private boolean gameStarted = false; MenuList(Maze3DMIDlet midlet, boolean apiAvailable) { super("Maze3D", List.IMPLICIT); this.midlet = midlet; setFitPolicy(List.TEXT_WRAP_ON); // in case the 3D API is not available if (apiAvailable) { append("New Game", null); append("3D Properties", null); } // add an exit command addCommand(new Command("Exit", Command.EXIT, 0)); setCommandListener(this); }
Create a method that indicates if a command event has occurred.
public void commandAction(Command command, Displayable displayable) { if (command == List.SELECT_COMMAND) { int index = getSelectedIndex(); switch (index) { case 0: // new game midlet.newGame(); // add more commands the first time the game runs if (!gameStarted) { insert(1, "Top view", null); addCommand(new Command("Back", Command.BACK, 0)); gameStarted = true; } break; case 1: if (gameStarted) { midlet.switchView(); } else { midlet.show3DProperties(); } break; case 2: midlet.show3DProperties(); break; } } else if (command.getCommandType() == Command.BACK) { midlet.showMain(); } else if (command.getCommandType() == Command.EXIT) { midlet.quitApp(); } } void viewSwitched() { // switch the labels for normal/top view if (getString(1).equals("Top view")) { set(1, "Normal view", null); } else { set(1, "Top view", null); } } }
This is the standard screen for handling errors.
Create the ErrorScreen
class
file.
Import the required classes.
import javax.microedition.lcdui.*;
Write the
below code to create the ErrorScreen
class and set it
to extend Alert
.
class ErrorScreen extends Alert { private static Image image; private static Display display; private static ErrorScreen instance = null;
Set up the ErrorScreen object.
private ErrorScreen() { super("Error"); setType(AlertType.ERROR); setTimeout(5000); setImage(image); }
Create a method to initialize the image and the display.
static void init(Image img, Display disp) { image = img; display = disp; }
Create a method to show the error screen.
static void showError(String message, Displayable next) { if (instance == null) { instance = new ErrorScreen(); } instance.setTitle("Error"); instance.setString(message); display.setCurrent(instance, next); } }