Hey everybody
I have a small problem
The Game class is the main MIDlet and Board is a Canvas.
I can access the and draw on the Canvas from the Game class
But I can not go from the Canvas back to the Main MIDlet.
The Game MIDlet creates menus and options and the player can start the game from here. But I've created a softbutton to allow the user to go back to the main menu - but it gives me a null pointer excp.
That is I dont understand that the reference in Board dosnt link back to Game??
The Game.class
package BackG;
import javax.microedition.midlet.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
public class Game extends MIDlet implements CommandListener
{
public static Board instance_Board;
private static Game instance_Game;
private Board the_board; /
private boolean firstTime = true;
private boolean firstTimeUser = true;
public String userName;
private static String Initial_Text = "Enter user name here - max 10 characters"; // initial text for input to username
private Command BACK = new Command("Back",Command.BACK,0);
private Command EXIT = new Command("Exit",Command.EXIT,1);
private Command DONE = new Command("Done",Command.OK,1);
private Command currentCommand = null;
private Displayable currentDisplayable;
private Display display;
private TextBox inputName;
private List mainMenu;
public List playMenu;
private List highscoreMenu;
private Form privateScore;
private Form top5;
String userScore;
String top5Score;
// string arrays for menus
String main[] =
{
"Username","Highscore","Play"
};
String highscore[]=
{
"Private Score", "Top 5"
};
String play[]=
{
"Play online","Local"
};
public Game()
{
super();
display = Display.getDisplay(this);
the_board = new Board();
highscoreMenu = new List("Highscore",Choice.IMPLICIT,highscore,null);
highscoreMenu.addCommand(BACK);
highscoreMenu.setCommandListener(this);
playMenu = new List("Chooce game",Choice.IMPLICIT,play, null);
playMenu.addCommand(BACK);
playMenu.setCommandListener(this);
inputName = new TextBox("Profile",Initial_Text,100,0);
inputName.addCommand(DONE);
inputName.addCommand(BACK);
inputName.setCommandListener(this);
userScore = "No scores stored";
top5Score = "No scores to see";
privateScore = new Form("Score");
privateScore.append(userScore);
privateScore.addCommand(DONE);
privateScore.setCommandListener(this);
top5 = new Form("Top 5 in the Wold!");
top5.append(top5Score);
top5.addCommand(DONE);
top5.setCommandListener(this);
firstTime = true;
instance_Board = the_board;
instance_Game = this;
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
inputName = null;
Initial_Text=null;
instance_Board = null;
}
public void quit()
{
destroyApp(true);
notifyDestroyed();
}
public void startApp()
{
if(firstTime==true)
{
mainMenu = new List("Main Menu",Choice.IMPLICIT,main,null);
mainMenu.addCommand(EXIT);
mainMenu.setCommandListener(this);
display.setCurrent(mainMenu);
firstTime= false;
}
}
public static Board getMIDlet()
{
return instance_Board;
}
public static Game getGame()
{
return instance_Game;
}
public void commandAction(Command c, Displayable d)
{
currentCommand = c;
currentDisplayable = d;
String com_type = currentCommand.getLabel();
if(com_type=="Back")
{
if(
currentDisplayable.equals(inputName) ||
currentDisplayable.equals(highscoreMenu) ||
currentDisplayable.equals(playMenu)
)
{
display.setCurrent(mainMenu); // show the mainMenu
}
}
else if(com_type=="Exit")
{
quit();
}
else if (com_type=="Done")
{
if(currentDisplayable.equals(inputName))
{
userName = inputName.getString();
firstTimeUser = false;
display.setCurrent(mainMenu);
}
else if (currentDisplayable.equals(privateScore) || currentDisplayable.equals(top5))
{
display.setCurrent(highscoreMenu);
}
}
else if (c== List.SELECT_COMMAND)
{
if(currentDisplayable.equals(mainMenu)) // options from the mainMenu
{
switch (mainMenu.getSelectedIndex())
{
case 0:
if(firstTimeUser == false)
{
inputName= new TextBox("Profile",userName,10,0);
inputName.addCommand(DONE);
inputName.addCommand(BACK);
inputName.setCommandListener(this);
}
display.setCurrent(inputName);
break;
case 1:// if the shown list is Submenu2
display.setCurrent(highscoreMenu);
break;
case 2:
display.setCurrent(playMenu);
break;
}
}
else if(currentDisplayable.equals(highscoreMenu)) //options from the highscoreMenu
{
switch (highscoreMenu.getSelectedIndex())
{
case 0:
display.setCurrent(privateScore);
break;
case 1:
display.setCurrent(top5);
break;
}
}
else if (currentDisplayable.equals(playMenu))
{
switch(playMenu.getSelectedIndex())
{
case 0:
display.setCurrent(the_board);
break;
case 1:
display.setCurrent(the_board);
break;
}
}
}
}//end commandAction
/*public void playMenu()
{
display.setCurrent(playMenu);
}*/
} // end Game.class
---------------------------------------------------------------------------------------
The Board.class
package BackG;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Board extends Canvas implements CommandListener
{
private Game the_game;
private Command BACK = new Command("Back",Command.BACK,0);
//private Command EXIT = new Command("Exit",Command.EXIT,0);
private Command CLEAR = new Command("Clear",Command.OK,1);
private Display display;
private static final int WHITE = 0xFF << 16 | 0xFF << 8 | 0xFF;
public Board()
{
// There must be a call to super because this is the Canvas.class no arg constructor
super();
addCommand(BACK);
//addCommand(EXIT);
addCommand(CLEAR);
setCommandListener(this);
}
public void commandAction(Command c, Displayable d)
{
if(c==BACK)
{
display.setCurrent(the_game.playMenu);
}
/*else if(c==EXIT)
{
// quit();
}*/
else if(c==CLEAR)
{
}
}
public void clear (Graphics g)
{
int clipX = g.getClipX();
int clipY = g.getClipY();
int clipH = g.getClipHeight();
int clipW = g.getClipWidth();
int color= g.getColor();
g.setColor(WHITE);
g.fillRect(clipX,clipY,clipW,clipH);
g.setColor(color);
}
public void paint(Graphics g)
{
//graphics
clear(g);
int height = getHeight();
int width = getWidth();
g.drawLine(20,13,width-20,height-31);
}
}
Any help is appreciated

Reply With Quote


