Using J2ME technology to develop the key processing mechanism of RPG game

Source: Internet
Author: User
Tags thread

In the game, the key processing mechanism also needs careful implementation, here is a practical key processing mechanism.

In the actual game, generally for the key-sensitive, we generally do not directly in the keypressed or Keyreleased method to write the logic of the code, but only in these methods to record or clear key records, and the actual processing in the thread. This is the approach adopted in this mechanism.

And the key values of different mobile phones are different, in order to facilitate the transplant, we convert the keys to their own defined values, and then in the program using a custom value for processing.

The most important variable in the mechanism is;

private int keystates;

Use one of the bits in the variable to represent whether a key is pressed, or 0 if pressed to 1. Each key is defined by itself, and the code defined is as follows:

The method of converting key values according to the mobile phone model is different, there are many differences, the following is the implementation of the WTK Simulator code:

When the key is pressed, the key value of the physical key is converted to a custom key value, then the key information is saved to the key state variable keystates, and the bitwise operator bit or implementation is used when saving. The implementation code is as follows:

When the key is released, it is similar to pressing the key, first convert the value, and then clear the key information. When cleared, the key state is reversed, and then the keystates bit and then. The implementation code is as follows:

In the interface switch, you need to clear the button state, so that only need to keystates clear 0. The implementation code is as follows:

The actual key-handling code can be implemented in a thread.

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.