Solve continuous buttons in J2ME development

xiaoxiao2021-03-06  17

We know that the low-level event processing in MIDP is processed by keypressed (), keyreleased () and keyrepeated (), which are triggered when the button is pressed, released, and repeated buttons. When the method is called, the system passes the key value of the pressed button to the above-mentioned three methods, and the key value of the button can perform related processing. The following key values ​​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, and key_pound.

In the game development, in order to ensure the portability of the program, we typically convert the key value to the game action, define the following game action in the MIDP: 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.

In general, KeyPressed () and keyreleased () are generally easier, but the processing buttons have been pressed slightly more complicated. Because the equipment we use does not necessarily support the continuous button event. You can detect whether the platform is supported when the platform is supported when the button is pressed. If you support, you can handle relevant logic in the KeyRepeated () method, if you don't support, then you must take other methods.

Here, the author introduces a method of processing a continuous button by setting the flag bit. In fact, the principle is very simple, we determine whether the button is pressed by setting the flag bit, such as what we judge that Left is pressed. When Left is pressed, 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;

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 = ";

Case Right: rightpressed = false; button; default: Break;} repaint ();

}

In this way, we can draw according to the state of the label 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 side, when the user presses Right, the J2ME string moves to the right. Simple, I didn't deal with Down and Up. Below is the application screenshot and source code.

Package com.j2medev;

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 {

}

}

Package com.j2medev;

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 () {if (leftpressed) {left ();} if (rightpressed) {right ();} try {thread.sleep (50);} catch (interruptedException 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.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 = ";

Case Right: rightpressed = false; button; 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;

DEFAULT: BREAK;

} Repaint ();

}

Public void keyrepeated (int keycode) {int action = getGameAction

DEFAULT: BREAK;} repaint ();

}

转载请注明原文地址:https://www.9cbs.com/read-44545.html

New Post(0)