| package net.frog_parrot.jump; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * This is the main class of the Tumbleweed game. * * @author Carol Hamer */ public class Jump extends MIDlet implements CommandListener { //--------------------------------------------------------- // Commands /** * The command to end the game. */ private Command myExitCommand = new Command("Exit", Command.EXIT, 99); /** * The command to start moving when the game is paused. */ private Command myGoCommand = new Command("Go", Command.SCREEN, 1); /** * The command to pause the game. */ private Command myPauseCommand = new Command("Pause", Command.SCREEN, 1); /** * The command to start a new game. */ private Command myNewCommand = new Command("Play Again", Command.SCREEN, 1); //--------------------------------------------------------- // Game object fields /** * The canvas that all of the game will be drawn on. */ private JumpCanvas myCanvas; /** * The thread that advances the cowboy. */ private GameThread myGameThread; //----------------------------------------------------- // Initialization and game state changes /** * Initialize the canvas and the commands. */ public Jump() { try { myCanvas = new JumpCanvas(this); myCanvas.addCommand(myExitCommand); myCanvas.addCommand(myPauseCommand); myCanvas.setCommandListener(this); } catch(Exception e) } /** * Switch the command to the play again command. */ void setNewCommand () /** * Switch the command to the go command. */ private void setGoCommand() /** * Switch the command to the pause command. */ private void setPauseCommand () //---------------------------------------------------------------- // Implementation of MIDlet. // These methods may be called by the application management // software at any time, so you always check fields for null // before calling methods on them. /** * Start the application. */ public void startApp() throws MIDletStateChangeException { if(myCanvas != null) { if(myGameThread == null) { myGameThread = new GameThread(myCanvas); myCanvas.start(); myGameThread.start(); } else } } /** * Stop and throw out the garbage. */ public void destroyApp(boolean unconditional) throws MIDletStateChangeException { if(myGameThread != null) myGameThread = null; myCanvas = null; System.gc(); } /** * Request the thread to pause. */ public void pauseApp() { if(myCanvas != null) If(myGameThread != null) } //---------------------------------------------------------------- // Implementation of CommandListener /* * Respond to a command issued on the Canvas. * (either reset or exit). */ public void commandAction(Command c, Displayable s) { if(c == myGoCommand) else if(c == myPauseCommand) else if(c == myNewCommand) else if((c == myExitCommand) || (c == Alert.DISMISS_COMMAND)) { try catch (MIDletStateChangeException ex) } } //------------------------------------------------------- // Error methods /** * Converts an exception to a message and displays * the message. */ void errorMsg(Exception e) { if(e.getMessage() == null) else { errorMsg(e.getClass().getName() + ":" + e.getMessage()); } } /** * Displays an error message alert if something goes wrong. */ void errorMsg(String msg) { Alert errorAlert = new Alert("error", msg, null, AlertType.ERROR); errorAlert.setCommandListener(this); errorAlert.setTimeout(Alert.FOREVER); Display.getDisplay(this).setCurrent(errorAlert); } } |
用户评论