Comparison of three low-level user interface event processing techniques in J2ME

Source: Internet
Author: User
Tags comparison thread
Comparison in the J2ME of low-level user interface programming, event handling is one of the most frequently used techniques, because the processing of mobile phone keyboard key event is the most commonly used, so take this as the center, introduce the actual use of the process of three ways to explain:

1, Cover keypressed method

In the interface that inherits the canvas, if you need to handle the key, we only need to overwrite the canvas class inside this method, when the phone button is pressed, the system will automatically call the method, and the pressed button key value (KeyCode) as a parameter passed in. So if you need to handle key events, just check the key values of the pressed keys inside the method.

In the mobile phone keyboard, can be simply divided into functional key area and the number of key areas, function key refers to the mobile phone on the soft key, the middle of the navigation key and answer the phone and hanging machine keys, and so on, the number key area refers to the mobile phone keypad on the 0-9 number keys as well as The number of function keys and key values, different mobile phones, and the number of keys in the area of the number of keys, as well as key values are the same.
 
There are two methods of detecting key values:

A) direct use of keycode values

The code that uses the KeyCode value directly has the following two kinds, respectively:

if (keycode = = 52) {
Handling code
}
Or:

if (keycode = = canvas.key_num4) {
Handling code
}
The above is the use of the key to the two code writing style, it is recommended that the actual use of the second type, so that the code easy to read. In the actual game development process, in addition to some special function keys, such as the phone on the left and right soft keys, rarely used this way to write, but the following way to deal with.

b) using gameaction (game action)

Gameaction is a mobile phone keyboard mapping into a game action mechanism, through this mechanism, can make the buttons on different phones can be converted to similar or the same game action, of course, this work is implemented by each handset manufacturer.

The code that is actually in use is as follows:

Convert KeyCode to Game action
int action = getgameaction (KeyCode);
The corresponding relationship between KeyCode and game action is as follows:

canvas.up--number key 2 and up navigation key
 
canvas.down--number keys 8 and down navigation keys

canvas.left--number keys 4 and left navigation keys

canvas.right--number key 6 and right navigation key

canvas.fire--number key 5 and OK navigation keys

Game_a, Game_b, Game_c, game_d, respectively, correspond to 1, 3, 7, 9, or 7, 9, *, and #键 on the keyboard.

The following event processing code is converted to a game action:

Switch (action) {
Case canvas.left://Left
Handling code
Break
Case Canvas right://to the right
Break
Case canvas.up://up
Break
......
}
This is the most commonly used key-handling method in MIDP1.0. But no matter how superior this way is, it still cannot get rid of the bondage of keypressed method.

This will be in the game programming three kinds of logic: delay, response user action, redraw, isolated, in the user thread and system thread.

In order to solve this problem, MIDP2.0 introduced a new event-handling mode key state (keystates) mechanism within Gamecanvas.

2. Use Keystates

The key state refers to the system through a variable to record the mobile phone keyboard on each and every game action related to the key, if the key pressed then set the corresponding bit (bit) is 1, otherwise 0, and then the bitwise operation can be obtained which button is pressed.

Note: The key state can only be used inside the MIDP2.0 Gamecanvas subclass and can only handle key-related keys. If you support this process to handle the game keys, you will not receive the game-related keys in the Keypressed method, and the other keys can receive the response normally.

If you want to use this feature, first of all, in the construction method of the Gamecanvas subclass, let the interface support the operation, which can be implemented using the Gamecanvas construction method as follows:

Enable the interface to support key state processing
Super (TRUE);
This allows the interface to be handled in key state. Unlike the previous keystroke handling, this approach can be separated from the limitations of the keypressed method, that is, it can be written in any of the processing methods, only to be called in the thread.

Using this approach, the system can maintain the state of the key regardless of how long the delay is in the thread. The advantage is that other ways of handling it are not possible.

First, get the game button status, the code is as follows:

Get the button status of the game
int states = Getkeystates ();
The function of the code is to get the current button status and clear the button status! The specific processing code is as follows:

Process Key Status:
if ((States & Gamecanvas.left_pressed)!= 0) {//Left
}
if (states & Gamecanvas. right_pressed)!= 0) {//Right
}
In MIDP2.0 's Game API processing, this approach allows mobile game development to return to the traditional model and make the game more flexible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.