The difference between public int getkeycode (int gameaction) and public int getgameaction (int keycode)

Source: Internet
Author: User
Tags abstract key
Distinguishes an MIDlet application by calling the canvas method to detect which keyboard code maps to an abstract game action in a running application: public static int getgameaction (int keycode);  The canvas class defines abstract game action sets: up, down, left, right, fire, and so on. Game developers should be aware of a problem in the MIDP 1.0 specification. This class defines a way to convert keyboard code to game action, and also defines a way to convert game action to keyboard code. The public int getgameaction (int keycode) public int getkeycode (int gameaction) method getKeyCode (int gameaction) may cause problems. Since it can only return a keyboard code based on game actions, even MIDP 1.0 allows more than one keyboard code to be implemented. In Nokia mobile phones, some of the keyboard code is mapped to the same game action, such as "Up Key" and "2 Keys" are mapped to the up game action. This method can only return one of these, and the value returned is a specific implementation. However, if the method getgameaction (int keycode) uses the keyboard code of "Up Key" and "2 Keys" as a parameter, this method returns the correct upward game action. Let's look at a bad example to deepen our impression:
Bad example, do not do this: class Tetriscanvas extends Canvas {int Leftkey, Rightkey, Downkey, Rotatekey;        void Init () {//following must is done Leftkey = getKeyCode (left);        Rightkey = getKeyCode (right);        Downkey = getKeyCode (down);    Rotatekey = getKeyCode (FIRE); }

public void keypressed (int keycode) {if (keycode = = Leftkey) {moveblockleft ();    else if (keycode = Rightkey) {...} }}
Here's a better solution:
Class Tetriscanvas extends Canvas {void init () {} public void keypressed (int keycode) {int action = Get        Gameaction (KeyCode);                Switch (action) {case Canvas.LEFT:moveBlockLeft ();            Break                Case Canvas.RIGHT:moveBlockRight ();        Break }}}

This example is an example of the MIDP 1.0 specification, which uses getkeycode (int gameaction) to handle keyboard code values and can only return one value. If this is the case, other possible key mappings cannot be used in MIDlet. For example, in the case of Nokia 7650, Nokia 7650 has five arrow keys and a joystick and a normal keyboard layout, and this example returns the value of the joystick rather than the value of the keyboard. This is a device-independent approach to handling events and is a bad way to do it. A better solution is to use getgameaction (int keycode) within the keypressed () method. Typically, an application should avoid using the getKeyCode (int gameaction) method and always use getgameaction (int keycode).

From: yesky.com


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.