• Advertisement
Sign in to follow this  

(MIDP1.0) - Adding soft keys to class extending Canvas

This topic is 4946 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a game, Crates3D, for my cellphone. I've got a basic menu working for the main intro bit using a form (It shows a title pic and has a menu with two items: new game and about). However, when I go to the main game I'm in a class called GameScreen which extends Canvas for all it's graphical goodness. How can I populate the white space at the bottom of the screen with softkey buttons? I tried just creating a form but I get all sorts of weird and wonderful errors, and when I tried to create a form with a canvas in another class that extended MIDlet it brought up "Canvas is abstract and couldn't be instantiated". Help!

Share this post


Link to post
Share on other sites
Advertisement
You can just add Commands to the Canvas subclass:

public class GameScreen extends Canvas implements CommandListener {

// ...

Command menu = new Command("Menu",Command.SCREEN,1);
Command quit = new Command("Quit",Command.EXIT,1);

public GameScreen(...) {
addCommand(menu);
addCommand(quit);
setCommandListener(this);
// ...
}

// ...

public void commandAction(Command c, Displayable d) {
if (c == menu) {
goToMenu();
}
else if (c == quit) {
quitGame();
}
}
}




You don't have to implement the CommandListener in GameScreen either. Some people prefer to have their Command handling centralized in one class, so you could just set the CommandListener to that class instead.

shmoove

Share this post


Link to post
Share on other sites
Ah, that makes a bit more sense.
I was trying to add commands to a menu.
The whole concept of Classes is still a bit new to me (having come through BBC BASIC, VB6, QB, z80 Assembler) and so I'm finding things a little tricky :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement