A Simple Form

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

A Simple Form

Postby Qi Hui » Fri Oct 20, 2006 8:02 am

Hi I am new to J2ME and this is a simple Coding that i edited but somehow the back button doesnt work.

Code: Select all
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class EventEx1 extends MIDlet
   implements CommandListener {
    // display manager
    Display display = null;
   
    // a menu with items
    List menu = null; // main menu

    // textbox
    TextBox input = null;

    // command
    static final Command backCommand =
   new Command("Back", Command.BACK, 0);
    static final Command mainMenuCommand =
   new Command("Main", Command.SCREEN, 1);
    static final Command exitCommand =
   new Command("Exit", Command.STOP, 2);
    String currentMenu = null;

    // constructor.
    public EventEx1() {
    }

    /**
     * Start the MIDlet by creating a list of items
     * and associating the exit command with the list.
     */
    public void startApp()
      throws MIDletStateChangeException {
      display = Display.getDisplay(this);

      menu = new List("Main Menu", Choice.IMPLICIT);
      menu.append("New Game", null);
      menu.append("Saved Game", null);
      menu.append("High Score", null);
      menu.append("Instructions", null);
      menu.append("Settings", null);
      menu.addCommand(exitCommand);
      menu.setCommandListener(this);

      mainMenu();
    }

    public void pauseApp() {
      display = null;
      menu = null;
      input = null;
    }

    public void destroyApp(boolean unconditional) {
      notifyDestroyed();
    }

    // main menu
    void mainMenu() {
      display.setCurrent(menu);
      currentMenu = "Main Menu";
    }

    /**
     * a generic method that is called when any of
     * the items on the list are selected.
     */
    public void prepare() {
       input = new TextBox("Enter some text: ",
      "", 6, TextField.ANY);
       input.addCommand(backCommand);
       input.setCommandListener(this);
       input.setString("");
       display.setCurrent(input);
       
    }

    /**
     * Test item1.
     */
    public void testItem1() {
      prepare();
      currentMenu = "New Game";
    }
   
    /**
     * Test item2.
     */
    public void testItem2() {
       prepare();
       currentMenu = "Saved Game";
    }

    /**
     * Test item3.
     */
    public void testItem3() {
       prepare();
       currentMenu = "High Scores";
    }

    /**
     * Test item4.
     */
    public void testItem4() {
       prepare();
       currentMenu = "Instructions";
    }
/**
* Test item5.
*/
    public void testItem5() {
        prepare();
        currentMenu = "Settings";
    }
   /**
    * Handle events.
    */ 
   public void commandAction(Command c, Displayable d) {
      String label = c.getLabel();
      if (label.equals("Exit")) {
         destroyApp(true);
      } else if (label.equals("Back")) {
          if(currentMenu.equals("item1") ||
   currentMenu.equals("item2") ||
        currentMenu.equals("item3") ||
        currentMenu.equals("item4") ||
        currentMenu.equals("item5")){
            // go back to menu
            mainMenu();
          }
         
      } else {
         List down = (List)display.getCurrent();
         switch(down.getSelectedIndex()) {
           case 0: testItem1();break;
           case 1: testItem2();break;
           case 2: testItem3();break;
           case 3: testItem4();break;
           case 4: testItem5();break;
         }
      }
  }
}
[/code]
Qi Hui
 
Posts: 3
Joined: Fri Oct 20, 2006 7:16 am

Check the Code

Postby silvercoder » Fri Oct 27, 2006 10:26 am

Code: Select all
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class EventEx1 extends MIDlet
        implements CommandListener {
    Display display = null;
    List menu = null;
    TextBox input = null;
   
    static final Command backCommand =
            new Command("Back", Command.BACK, 0);
    static final Command mainMenuCommand =
            new Command("Main", Command.SCREEN, 1);
    static final Command exitCommand =
            new Command("Exit", Command.STOP, 2);
    String currentMenu = null;
   
    public EventEx1() {
    }
   
    public void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
       
        menu = new List("Main Menu", Choice.IMPLICIT);
        menu.append("New Game", null);
        menu.append("Saved Game", null);
        menu.append("High Score", null);
        menu.append("Instructions", null);
        menu.append("Settings", null);
        menu.addCommand(exitCommand);
        menu.setCommandListener(this);

        mainMenu();
    }
   
    public void pauseApp() {
        display = null;
        menu = null;
        input = null;
    }
   
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
   
    void mainMenu() {
        display.setCurrent(menu);
        currentMenu = "Main Menu";
    }
   
    public void prepare() {
        input = new TextBox("Enter some text: ", "", 6, TextField.ANY);
        input.addCommand(backCommand);
        input.setCommandListener(this);
        input.setString("");
        display.setCurrent(input);
       
    }
   
    public void testItem1() {
        prepare();
        currentMenu = "New Game";
    }
   
    public void testItem2() {
        prepare();
        currentMenu = "Saved Game";
    }
   
    public void testItem3() {
        prepare();
        currentMenu = "High Scores";
    }
   
    public void testItem4() {
        prepare();
        currentMenu = "Instructions";
    }

    public void testItem5() {
        prepare();
        currentMenu = "Settings";
    }

    public void commandAction(Command c, Displayable d) {
        String label = c.getLabel();
        if (label.equals("Exit")) {
            destroyApp(true);
        } else if (label.equals("Back")) {
            if(currentMenu.equals("New Game") ||
                    currentMenu.equals("Saved Game") ||
                    currentMenu.equals("High Scores") ||
                    currentMenu.equals("Instructions") ||
                    currentMenu.equals("Settings")){
                // go back to menu
                mainMenu();
            }
           
        } else {
            List down = (List)display.getCurrent();
            switch(down.getSelectedIndex()) {
                case 0: testItem1();break;
                case 1: testItem2();break;
                case 2: testItem3();break;
                case 3: testItem4();break;
                case 4: testItem5();break;
            }
        }
    }
}




I have changed the command action function alone, check it, The modification is u didnt find correctly in which current menu the back command is there!!!! If still having doubt kindly reply i shall clear you......[:)]
silvercoder
 
Posts: 37
Joined: Fri Sep 29, 2006 12:02 pm

A Simple Form

Postby Qi Hui » Sun Oct 29, 2006 2:06 pm

Haha ic.. the application final works... Erm.. may i ask if you know how to connect a game canvas to a midlet when there are also other classes connected to the midlet??
Qi Hui
 
Posts: 3
Joined: Fri Oct 20, 2006 7:16 am

Postby DevelopmentTeam » Mon Oct 30, 2006 5:00 am

The same way you call a canvas from midlet, can be used for gamecanvas even.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India


Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 2 guests

cron