Game Actions

Team-Fly

Key codes may be useful in certain situations, but they're fairly specific to a device. MIDP offers a simple abstraction called a game action that makes it easier to map user key events to events that will be useful for games and other applications with specialized user interfaces.

The concept is simple: Supply a key code to getGameAction(), and you'll receive a game action-one of the following values: UP, DOWN, LEFT, RIGHT, FIRE, GAME_A, GAME_B, GAME_C, GAME_D. Basically game actions are a way to map the physical keys on a device to a set of video game buttons such as you might find on game platforms like Sega Genesis or Nintendo Game Boy.

To understand how this maps to a physical device, think about how you might map the UP, DOWN, LEFT, and RIGHT game actions to keys. On Sun's MIDP emulator, there are navigation keys that could easily be used for these game actions. Think about a simpler phone, however, one that has only a numeric keypad. In this case, you might want to map UP to the 2 key, DOWN to the 8 key, LEFT to the 4 key, and RIGHT to the 6 key.

Using game actions saves you from having to make these decisions yourself; the MIDP implementation simply provides a reasonable mapping for the device. To find the game action for a key code, pass the key code to getGameAction(). You can also find the key code for a game action by calling getKeyCode().

The following example listens for key presses in the keyPressed() method. It converts the key code to a game action and displays the game action on the screen.

 import javax.microedition.lcdui.*; public class KeyCanvas     extends Canvas {   private Font mFont;   private String mMessage = "[Press keys]";   public KeyCanvas() {     mFont = Font.getFont(Font.FACE_PROPORTIONAL,         Font.STYLE_PLAIN, Font.SIZE_MEDIUM);   }   public void paint(Graphics g) {     int w = getWidth();     int h = getHeight();     // Clear the Canvas.     g.setGrayScale(255);     g.fillRect(0, 0, w − 1, h − 1);     g.setGrayScale(0);     g.drawRect(0, 0, w − 1, h − 1);     g.setFont(mFont);     int x = w / 2;     int y = h / 2;     g.drawString(mMessage, x, y, Graphics.BASELINE | Graphics.HCENTER);   }   protected void keyPressed(int keyCode) {     int gameAction = getGameAction(keyCode);     switch(gameAction) {       case UP:     mMessage = "UP";             break;       case DOWN:   mMessage = "DOWN";           break;       case LEFT:   mMessage = "LEFT";           break;       case RIGHT:  mMessage = "RIGHT";          break;       case FIRE:   mMessage = "FIRE";           break;       case GAME_A: mMessage = "GAME_A";         break;       case GAME_B: mMessage = "GAME_B";         break;       case GAME_C: mMessage = "GAME_C";         break;       case GAME_D: mMessage = "GAME_D";         break;       default:     mMessage = ""; break;     }     repaint();   } } 

To run this example, you'll need a corresponding MIDlet to display KeyCanvas. At this point, I think you can do this by yourself.


Team-Fly


Wireless Java. Developing with J2ME
ColdFusion MX Professional Projects
ISBN: 1590590775
EAN: 2147483647
Year: 2000
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net