We know that the low-level event processing in MIDP is through keypressed (), keyreleased () and keyrepeated
() Troubled at the button to be pressed, released, and repeated buttons, respectively. When the method is called, the system
The key value of the pressed button will be passed to the above three methods, and we can perform related processing according to the key value of the button. in
The following buttons are defined in the 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.
In the game development, in order to ensure the portability of the program, we usually turn the key value to the game action, in the MIDP
The following game action is defined: Up, Down, Left, Right, Fire, Game_a, Game_b,
Game_c, game_d. Conversion is very simple, you can getGetGameAction () through the method provided by Canvas.
Generally speaking, KeyPressed () and Keyreleased () are easier, but the processing button has been pressed.
The situation is slightly more complicated. Because the equipment we use does not necessarily support the continuous button event. You can pass the method
HASREPEATEVENTS () to detect whether the platform is supported when the button is pressed. If supported
You can handle relevant logic in the KeyRepeated () method, if you don't support, then you must take another method.
Here, the author introduces a method of processing a continuous button by setting the flag bit. In fact, the principle is very simple.
We judge whether the button is pressed by setting the flag bit. For example, we judge that the Left is pressed. When Left is
When you press it, we set the member variable leftpresseded to true, the code is as follows:
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 ();
}
When the button is released, we set the relevant marking bit to 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 ();
}
In this way, we can paint according to the state of the marking bit when re-drawing the screen:
IF (leftpressed)
{
LEFT ();
}
IF (Rightpressed)
{
Right ();
}
The author gives a simple instance to argue, we make a MIDlet, when the user presses Left,
The J2ME string moves to the left, when the user presses Right, the J2ME string moves to the right. Simple, I
There is no situation in which DOWN and UP are processed. Here is the application source code.
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 MIDLETSTATECHANGEXCEPTION
{
}
}
Package com.j2medev;
Import javax.microedition.lcdui. *;
Public Class Maincanvas Extends Canvas IMPLEments Runnable
{
PRIVATE STRING button PPRESSED
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 (InterruptedExcect e)
{
E.PrintStackTrace ();
}
}
}
Public void Paint (Graphics G)
{
G.SetColor (0xfffffff);
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 ();
}
}