| Reply | « Previous Thread | Next Thread » |
|
Hello
I wonder is there some not very complex way to implement menu similar to built-in Snake game. My game uses FullCanvas, which does not allow any Commands. In Snake game pressing Soft Key while playing on full screen will show you a menu OVER a graphical image. Is there a way to make similar in Java? Thanx |
|
Hi,
the basic idea is to paint your command items as strings on the canvas and then just go catching keypresses. Repaint the canvas after each keypress (down/up for moving and fire for menu item selection (KEY_SOFTKEY1 in case of Series 40 device)) and there you go. Of course your needs might vary from what this example offers, feel free to customize paint() method as needed. Just one approach: package uimodel_s60; import javax.microedition.lcdui.*; import com.nokia.mid.ui.*; /** * <p>Title: Game UI Model</p> * <p>Description: Game UI model for Series 60.</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: Forum Nokia</p> * @author tinke * @version 1.0 */ public class MenuFullCanvas extends FullCanvas { private UIModelMIDlet parent = null; private Manager manager = null; private int WIDTH = getWidth(); private int HEIGHT = getHeight(); private int MENU_LENGTH = 6; private String[] items = new String[MENU_LENGTH]; private int index = 1; public MenuFullCanvas(UIModelMIDlet parent, Manager manager) { this.parent = parent; this.manager = manager; items[0] = Resources.getString(Resources.ID_MAIN_CONTINUE); items[1] = Resources.getString(Resources.ID_MAIN_NEW_GAME); items[2] = Resources.getString(Resources.ID_MAIN_SETTINGS); items[3] = Resources.getString(Resources.ID_MAIN_HIGH_SCORES); items[4] = Resources.getString(Resources.ID_MAIN_HELP); items[5] = Resources.getString(Resources.ID_MAIN_ABOUT); if (manager.isPaused() == true) { index = 0; } else { index = 1; } } protected void paint(Graphics g) { g.setColor(0, 0, 255); g.fillRect(0, 0, WIDTH, HEIGHT); g.setColor(255, 255, 0); g.setFont(manager.FONT_LARGE); g.drawString(Resources.getString(Resources.ID_TITLE_MAIN_MENU), WIDTH/2, 2, Graphics.HCENTER | Graphics.TOP); g.fillRect(0, 32 + (index * (manager.FONT_MEDIUM.getHeight() + 2)) - 2, WIDTH, 17); g.setFont(manager.FONT_MEDIUM); if (manager.isPaused() == true) { g.setColor(0, 0, 0); g.drawString(Resources.getString(Resources.ID_MAIN_CONTINUE), WIDTH/2 + 1, 32, Graphics.HCENTER | Graphics.TOP); g.setColor(255, 255, 255); g.drawString(Resources.getString(Resources.ID_MAIN_CONTINUE), WIDTH/2, 32, Graphics.HCENTER | Graphics.TOP); } for (int i=1; i < MENU_LENGTH; i++) { g.setColor(0, 0, 0); g.drawString(Resources.getString((Resources.ID_MAIN_CONTINUE + i)), WIDTH/2 + 1, 32 + (i * (manager.FONT_MEDIUM.getHeight() + 2)), Graphics.HCENTER | Graphics.TOP); g.setColor(255, 255, 255); g.drawString(Resources.getString((Resources.ID_MAIN_CONTINUE + i)), WIDTH/2, 32 + (i * (manager.FONT_MEDIUM.getHeight() + 2)), Graphics.HCENTER | Graphics.TOP); } } protected void keyPressed(int keyCode) { if (keyCode == KEY_SOFTKEY1) { selectIndex(index); } if (keyCode == KEY_SOFTKEY2) { parent.notifyDestroyed(); } switch (getGameAction(keyCode)) { case UP: index--; if ((index == 0 && manager.isPaused() == false) || index < 0) index = MENU_LENGTH - 1; repaint(); break; case DOWN: index++; if (index == MENU_LENGTH) { if (manager.isPaused() == true) { index = 0; } else { index = 1; } } repaint(); break; case FIRE: selectIndex(index); break; default: break; } } protected void keyRepeated(int keyCode) { switch (getGameAction(keyCode)) { case UP: index--; if ((index == 0 && manager.isPaused() == false) || index < 0) index = MENU_LENGTH - 1; repaint(); break; case DOWN: index++; if (index == MENU_LENGTH) { if (manager.isPaused() == true) { index = 0; } else { index = 1; } } repaint(); break; case FIRE: selectIndex(index); break; default: break; } } protected void hideNotify() { // nothing here } protected void showNotify() { // nothing here } private void selectIndex(int i) { } } Kind regards, Tinke / FN |
|
tinkezione, thank you.
I had a look at your example - looks like a good start for most arcade games :) However, it doen't compile at once. If ypu would like this thread to be used by novice developers it would be useful to place full code here (link to it) or place only very essential part of the code, with minimal references to other classes. Now about my case. I might use your approach for the very main game menu. But in my game there is lot's of local, context-based menus. That's why I would like to implement them as standard context-based menu. The ideal variant would be like Snake Ex menu. Now I show just a List. I can imagine painting menu looking like a standard one on the FullCanvas and shading content behind the menu. But it would be much easier if there is some ready template for it or some more standardized approach. E.g. if I could switch from FullCanvas to Canvas AND open Commands menu manually. |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|