我们知道MIDP中的低级事件处理是通过keyPressed()、 keyReleased()和keyRepeated()来处理的,分别在按键被按下、释放和重复按键的时候被触发。当方法被调用的时候,系统会把所按下键的键值传递给上述的三个方法,根据按键的键值我们可以进行相关的处理。在MIDP中定义了如下的按键值分别是: KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_STAR 和 KEY_POUND。
在游戏开发中为了保证程序的可移植性,通常我们都会把键值转换为游戏动作,在MIDP中定义了如下的游戏动作: UP, DOWN, LEFT, RIGHT, FIRE, GAME_A, GAME_B, GAME_C,GAME_D。转换非常简单,可以通过Canvas提供的方法getGameAction()。
一般来说处理keyPressed()和keyReleased()都比较容易,但是处理按键一直被按下的情况稍微复杂一些。因为我们使用的设备并不一定支持连续按键的事件。你可以通过方法hasRepeatEvents()来检测平台是否支持当按键持续按下的时候产生重复事件。如果支持那么你可以在 keyRepeated()的方法中处理相关逻辑,如果不支持那么你必须采取其他的方法。
这里笔者介绍一种通过设置标志位的方式来处理连续按键的方法。其实原理非常的简单,我们通过设置标志位判断按键是否被按下了,比如我们判断LEFT是不是被按下了。当LEFT被按下的时候,我们把成员变量leftPressed设置为true,代码如下:
public void keyPressed(int keyCode) { int action = getGameAction(keyCode); switch (action) { case LEFT: left(); leftPressed = true; break; case RIGHT: right(); rightPressed = true; break; default: break; } repaint(); }
当按键被释放的时候,我们就把相关的标记位设置为false。
public void keyReleased(int keyCode) { int action = getGameAction(keyCode); switch (action) { case LEFT: leftPressed = false; buttonPressed = ""; break; case RIGHT: rightPressed = false; buttonPressed = ""; break; default: break; } repaint(); }
这样我们在重新绘制屏幕的时候就可以根据标记位的状态进行绘画了
if (leftPressed) { left(); } if (rightPressed) { right(); }笔者给出一个简单的实例来进行论证,我们制作一个MIDlet,当用户按下LEFT的时候,J2ME字符串向左侧移动,当用户按下RIGHT的时候,J2ME字符串向右侧移动。简单起见,我没有处理DOWN和UP的情况。
import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class KeyActionMIDlet extends MIDlet { private Display display; private MainCanvas mainCanvas; protected void startApp() throws MIDletStateChangeException { display = Display.getDisplay(this); mainCanvas = new MainCanvas(); new Thread(mainCanvas).start(); display.setCurrent(mainCanvas); } protected void pauseApp() { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } } import javax.microedition.lcdui.*; public class MainCanvas extends Canvas implements Runnable { private String buttonPressed; private boolean leftPressed; private boolean rightPressed; private int px = getWidth() / 2; public final int py = getHeight() / 2; public MainCanvas() { buttonPressed = " "; } private void left() { if (px >= 0) { px--; } buttonPressed = "LEFT"; repaint(); } private void right() { if (px <= getWidth()) { px; } buttonPressed = "RIGHT"; repaint(); } public void run() { while (true) { if (leftPressed) { left(); } if (rightPressed) { right(); } try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } public void paint(Graphics g) { g.setColor(0xFFFFFF); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0x000000); g.drawString(buttonPressed, 20, 20, Graphics.LEFT | Graphics.TOP); g.drawString("J2ME", px, py, Graphics.HCENTER | Graphics.TOP); } public void keyReleased(int keyCode) { int action = getGameAction(keyCode); switch (action) { case LEFT: leftPressed = false; buttonPressed = ""; break; case RIGHT: rightPressed = false; buttonPressed = ""; break; default: break; } repaint(); } public void keyPressed(int keyCode) { int action = getGameAction(keyCode); switch (action) { case LEFT: left(); leftPressed = true; break; case RIGHT: right(); rightPressed = true; break; default: break; } repaint(); } public void keyRepeated(int keyCode) { int action = getGameAction(keyCode); switch (action) { case LEFT: left(); break; case RIGHT: right(); break; default: break; } repaint(); } }