FileDocCategorySizeDatePackage
WormMain.javaAPI DocJ2ME MIDP 2.04678Thu Nov 07 12:02:18 GMT 2002example.wormgame

WormMain

public class WormMain extends javax.microedition.midlet.MIDlet implements javax.microedition.lcdui.CommandListener
Main routine for worm MIDlet.

Fields Summary
private WormPit
theGame
Current game board for worm pit.
private javax.microedition.lcdui.Command
exitCmd
Button for exiting the game.
private javax.microedition.lcdui.Command
levelCmd
Menu item for changing game levels.
private javax.microedition.lcdui.Command
startCmd
Menu item for starting a new game.
private javax.microedition.lcdui.Command
restartCmd
Menu item to restart another game.
private javax.microedition.lcdui.Command
audioOnCmd
Enable audio.
private javax.microedition.lcdui.Command
audioOffCmd
Disable audio.
private javax.microedition.lcdui.Command
cancelCmd
Menu item to cancel current pausedmenu dialog.
private javax.microedition.lcdui.Command
OKCmd
Menu item to confirm current selected operation.
Constructors Summary
public WormMain()
Default mconstructor for worm MIDlet game. Creates the initial graphics objects and sets the command listener.

                         
      
	theGame = new WormPit();
	theGame.addCommand(exitCmd);
	theGame.addCommand(levelCmd);
	theGame.addCommand(startCmd);
	theGame.addCommand(audioOnCmd);
	theGame.setCommandListener(this);
    
Methods Summary
public voidcommandAction(javax.microedition.lcdui.Command c, javax.microedition.lcdui.Displayable d)
Respond to a commands issued on any Screen.

param
c command object source of action
param
d screen object containing actioned item

        if (c == restartCmd) {
	    theGame.restart();
        } else if (c == levelCmd) {
	    Item[] levelItem = {
		new Gauge("Level", true, 9, theGame.getLevel())};
	    Form f = new Form("Change Level", levelItem);
	    f.addCommand(OKCmd);
	    f.addCommand(cancelCmd);
	    f.setCommandListener(this);
	    Display.getDisplay(this).setCurrent(f);
        } else if (c == exitCmd) {
	    destroyApp(false);
	    notifyDestroyed();
        } else if (c == startCmd) {
	    theGame.removeCommand(startCmd);
	    theGame.addCommand(restartCmd);
	    theGame.restart();
        } else if (c == OKCmd) {
	    Form f = (Form)d;
	    Gauge g = (Gauge)f.get(0);
	    theGame.setLevel(g.getValue());
	    Display.getDisplay(this).setCurrent(theGame);
        } else if (c == cancelCmd) {
	    Display.getDisplay(this).setCurrent(theGame);
        } else if (c == audioOnCmd) {
	    /* Turn on Audio */
	    theGame.createAudioPlayer();
	    theGame.removeCommand(audioOnCmd);
	    theGame.addCommand(audioOffCmd);
        } else if (c == audioOffCmd) {
	    /* Turn off Audio */
	    theGame.destroyAudioPlayer();
	    theGame.removeCommand(audioOffCmd);
	    theGame.addCommand(audioOnCmd);
	}
    
protected voiddestroyApp(boolean unconditional)
Destroy must cleanup everything. Only objects exist so the GC will do all the cleanup after the last reference is removed.

param
unconditional if true, force MIDlet destroy processing

	theGame.destroyAudioPlayer();
	theGame.destroyGame();
	Display.getDisplay(this).setCurrent((Displayable)null);
    
protected voidpauseApp()
Pause signals the thread to stop by clearing the thread field. If stopped before done with the iterations it will be restarted from scratch later.

    
protected voidstartApp()
Start creates the thread to do the timing. It should return immediately to keep the dispatcher from hanging.

	Display.getDisplay(this).setCurrent(theGame);

	try {
	    // Start the game in its own thread
	    Thread myThread = new Thread(theGame);
	    myThread.start();
	} catch (Error e) {
	    destroyApp(false);
	    notifyDestroyed();
	}