3 Replies Latest reply: Nov 24, 2009 8:36 AM by 796262 RSS

    need help on mobile gaming development basic  {working the game main menu}

    843853
      package Assignment1;
      
      import java.io.IOException;
      import java.util.Random;
      import javax.microedition.lcdui.Canvas;
      import javax.microedition.lcdui.Display;
      import javax.microedition.lcdui.Graphics;
      import javax.microedition.lcdui.Image;
      import javax.microedition.lcdui.game.GameCanvas;
      import javax.microedition.lcdui.game.Sprite;
      
      
      public class a1Canvas extends GameCanvas implements Runnable{
          private Display display;
          private Sprite ufoSprite;
          private Image backgroundImage;
          
          private long frameDelay;
          private int ufoX;
          private int ufoY;
          private int ufoXDir;
          private int ufoXSpeed;
          private int ufoYSpeed;
          private Random rand;
          private Sprite[] roidSprite = new Sprite[3];
          private boolean gameOverState;
          
      
          //constructor
      
          public a1Canvas(Display display){
              super(true);
              this.display = display;
              frameDelay = 33;
              gameOverState = false;
          }
      
      
          public void start(){
              display.setCurrent(this);
              //start the animation thread
              
               rand = new Random();     // Initialize the random number generator
               ufoXSpeed = ufoYSpeed = 0; // Initialize the UFO and roids sprites
      
              try {
                  backgroundImage = Image.createImage("/background.jpg");
                  ufoSprite = new Sprite(Image.createImage("/ufo.png"));
      
                  //initialise sprite at middle of screen
                  ufoSprite.setRefPixelPosition(25,25);
                  ufoX = (int)(0.5 * (getWidth()- ufoSprite.getWidth()));
                  ufoY = (int)(0.5 * (getHeight()- ufoSprite.getHeight()));
                  ufoSprite.setPosition(ufoX, ufoY);
      
                  Image img = Image.createImage("/Roid.png");
                  roidSprite[0] = new Sprite(img, 42, 35);   //create 1st frame-animated asteroid sprite
                  roidSprite[1] = new Sprite(img, 42, 35);
                  roidSprite[2] = new Sprite(img, 42, 35);
      
      
      
              } catch (IOException ex) {
                  System.err.println("Failed to load images");
              }
      
              Thread t = new Thread(this);
              t.start();
          }
      
          public void run() {
      
              while (!gameOverState){
      
                  
                  update();
                  draw(getGraphics());
                  try{
                      Thread.sleep(frameDelay);
                  }catch(InterruptedException ie){}
                  
              }
              gotoMainMenu(getGraphics());
      
          }
      
      
      
          private void draw(Graphics graphics) {
              //clear background to black
              graphics.setColor(0, 0, 0);
              graphics.fillRect(0, 0, getWidth(), getHeight());
      
              //draw the background
              graphics.drawImage(backgroundImage, getWidth()/2, getHeight()/2, Graphics.HCENTER | Graphics.VCENTER);
              ufoSprite.paint(graphics);
              for(int i=0;i<3;i++){
                  roidSprite.paint(graphics);
      }
      flushGraphics(); //flush graphics from offscreen to on screen
      }

      private void update() {
      // Process user input to control the UFO speed
      int keyState = getKeyStates();

      if ( (keyState & LEFT_PRESSED) != 0 )
      ufoXSpeed--;
      else if ( (keyState & RIGHT_PRESSED) != 0 )
      ufoXSpeed++;
      if ( (keyState & UP_PRESSED) != 0 )
      ufoYSpeed--;
      else if ( (keyState & DOWN_PRESSED) != 0 )
      ufoYSpeed++;

      ufoXSpeed = Math.min(Math.max(ufoXSpeed, -8), 8);
      ufoYSpeed = Math.min(Math.max(ufoYSpeed, -8), 8);

      ufoSprite.move(ufoXSpeed, ufoYSpeed); // Move the UFO sprite
      checkBounds(ufoSprite); // Wrap UFO sprite around the screen

      // Update the roid sprites
      for (int i = 0; i < 3; i++) {
      roidSprite[i].move(i + 1, 1 - i); // Move the roid sprites
      checkBounds(roidSprite[i]); // Wrap asteroid sprite around the screen

      // Increment the frames of the roid sprites; 1st and 3rd asteroids spin in opposite direction as 2nd
      if (i == 1)
                roidSprite[i].prevFrame();
      else
                roidSprite[i].nextFrame();

      // Check for a collision between the UFO and roids
      if ( ufoSprite.collidesWith(roidSprite[i], true) ) {
      System.out.println("Game Over !");
      gameOverState = true;


      }//if
      }//for
      }
      public void stop(){
      gameOverState = true;
      }

      private void checkBounds(Sprite sprite) {
      // Wrap the sprite around the screen if necessary
      if (sprite.getX() < -sprite.getWidth())
           sprite.setPosition(getWidth(), sprite.getY());
      else if (sprite.getX() > getWidth())
           sprite.setPosition(-sprite.getWidth(), sprite.getY());

      if (sprite.getY() < -sprite.getHeight())
           sprite.setPosition(sprite.getX(), getHeight());
      else if (sprite.getY() > getHeight())
           sprite.setPosition(sprite.getX(), -sprite.getHeight());

      }

      private void gotoMainMenu(Graphics graphics) {
      graphics.setColor(0, 0, 0);
      graphics.fillRect(0, 0, getWidth(), getHeight());

      graphics.setColor(255, 0, 0);
      graphics.drawRect(0, 0, getWidth()-1, getHeight()-1);


      graphics.setColor(255, 255, 255);
      graphics.drawString("GAME NAME", getWidth()/2 -50, 30, Graphics.LEFT | Graphics.TOP);
      graphics.setColor(255, 0, 0);
      graphics.drawString("Main Menu", getWidth()/2 -50, 50, Graphics.LEFT | Graphics.TOP);
      graphics.setColor(0, 255, 0);
      graphics.drawString("Start Game", getWidth()/2 , 80, Graphics.LEFT | Graphics.TOP);
      graphics.drawString("Instructions", getWidth()/2 , 110, Graphics.LEFT | Graphics.TOP);
      flushGraphics();

      int keyState = getKeyStates();
      if ( (keyState & FIRE_PRESSED) != 0 ){
      System.out.println("game start");
      gameOverState = false;
      }
      }
      }
      The problem i am facing is : 
      the FIRE button is not functioning, i put a system.out.println and it didnt print.
      i am unsure whether i put the key input listener at the correct function
      this button is supposed to start the game again.
      
      What i want to do :
      i want to display the main menu when i launched the game
      when game is over, it will display back to the main menu again
      when i press the fire button it will play the game again.
      
      i hope someone can help me on this :)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        • 1. Re: need help on mobile gaming development basic  {working the game main menu}
          796262
          kdoom wrote:
          The problem i am facing is :
          the FIRE button is not functioning, i put a system.out.println and it didnt print.
          i am unsure whether i put the key input listener at the correct function
          this button is supposed to start the game again.

          What i want to do :
          i want to display the main menu when i launched the game
          when game is over, it will display back to the main menu again
          when i press the fire button it will play the game again.

          i hope someone can help me on this :)
          You didn't have to create a whole new post just for the formatted code, silly. You could have just replied in the old thread. Anyway:

          Step through the program and think about what each line is doing. Most importantly:
              public void run() {
                  while (!gameOverState){
                      update();
                      draw(getGraphics());
                      try{
                          Thread.sleep(frameDelay);
                      }catch(InterruptedException ie){}
                      
                  }
                  gotoMainMenu(getGraphics());
              }
          This says that while the game is being played, update( ), then draw( ). When the game is over, call gotoMainMenu( ).
             private void gotoMainMenu(Graphics graphics) {
          
                  //do a bunch of graphics stuff
          
                  int keyState = getKeyStates();
                  if ( (keyState & FIRE_PRESSED) != 0 ){
                      System.out.println("game start");
                      gameOverState = false;
                  }
              }
          This says to do a bunch of graphics stuff, and then to check the key states. If fire is being pressed, print something out and set gameOverState to false. This check only happens once. When the state is being checked, most likely the user will not be pressing any keys, so the code is done.

          Also, simply setting the gameOverState variable to false outside of the loop you use it in won't do anything. For example:
          boolean doLoop = true;
          int count = 0;
          while(doLoop){
             count++;
             if(count > 10){
                doLoop = false;
             }
          }
          doLoop = true;
          Would you expect that second doLoop = true to start the while loop over?
          • 2. Re: need help on mobile gaming development basic  {working the game main menu}
            843853
                private void update() {
            
                    int keyState = getKeyStates();
                    //Main Menu 0
                    if (gameState == 0) {
                        drawMainMenu(getGraphics());
                        updateMenu(keyState);
                    }
                    //Game 1
            
                    if (gameState == 1) {
                        drawGame(getGraphics());
                        updateGame(keyState);
                    }
            
                    //Instruction 2
                    if (gameState == 2) {
                        drawInstruction(getGraphics());
                        updateInstruction(keyState);
                    }
            
                    //Game Over Page 3
                    if (gameState == 3) {
                        drawGameOverPage(getGraphics());
                        updateGameOverPage(keyState);
                    }
                }
            
            
                private void updateGameOverPage(int keyState) {
            
                    while (gameState == 3) {
                        drawGameOverPage(getGraphics());
                        //go back to main menu
                        if ((keyState & FIRE_PRESSED) != 0) {
            
                            gameState = 0;
                            
                        }
                    }
            
                }
            i think the problem lies in the updateGameOverPage function getting called but when it didnt get a key input at that instant it skips the loop and stuck at that page. i dont know how to solve this
            • 3. Re: need help on mobile gaming development basic  {working the game main menu}
              796262
              kdoom wrote:
              i think the problem lies in the updateGameOverPage function getting called but when it didnt get a key input at that instant it skips the loop and stuck at that page. i dont know how to solve this
              I'm not extremely familiar with the mobile development stuff, but it seems like you want to keep calling the update( ) function, even when you're not in a game. That latest code seems to be what you're looking for, checking the game state (game over, game currently playing, in a menu, etc) before responding to key inputs. But for that to work, you want to keep calling update( ) even after the game is over. To do that, you'll have to modify your run( ) method.