Development of RPG games using j2-based technology (2) -- Key Processing Mechanism

Source: Internet
Author: User

Development of RPG games using j2-based technology (2) -- Key Processing Mechanism

Author: Chen yuefeng

From: http://blog.csdn.net/mailbomb

 

In the game, the key processing mechanism also needs to be carefully implemented. Here we will introduce a practical key processing mechanism.

In actual games, we generally do not write logic Code directly in the keypressed or keyreleased Method for key sensitivity. Instead, we only record or clear key records in these methods, and put the actual processing in the thread. This is the method used in this mechanism.

In addition, the key values of different mobile phones are different. For convenience of transplantation, we convert the key values into custom values, and then use custom values in the program for processing.

The most important variable in this mechanism is;

Private int keystates;

This variable uses a binary value to indicate whether a key is pressed. If the value is 1, otherwise, it is 0. Each key is defined by itself. The defined code is as follows:

/** Up */

Private Final int key_up = 1;

/** Down */

Private Final int key_down = 1 <1;

/** Right */

Private Final int key_right = 1 <2;

/** Left */

Private Final int key_left = 1 <3;

/** 5 Keys */

Private Final int key_fire = 1 <4;

/** Left soft key */

Private Final int key_left_soft = 1 <5;

/** Right soft key */

Private Final int up = 1 <6;

/** Special-purpose buttons, such as 0 keys */

Private Final int key_zero = 1 <7;

The method for converting key values varies depending on the phone model. The following is the implementation code of the wtk simulator:

/**

* Convert a physical key value to a custom key value

* Description: This method is related to the model. The following describes the implementation of wtk.

* @ Param keycode physical key value

* @ Return custom key value

*/

Private int convertkey (INT keycode ){

Switch (keycode ){

Case-6:

Return key_left_soft;

Case-7:

Return key_right_soft;

Case canvas. key_num2:

Case-1:

Return key_up;

Case canvas. key_num4:

Case-3:

Return key_left;

Case canvas. key_num6:

Case-4:

Return key_right;

Case canvas. key_num8:

Case-2:

Return key_down;

Case canvas. key_num0:

Return key_zero;

}

Return 0;

}

When you press the key, first convert the key value of the physical key to a custom key value, and then save the key information to the key state variable keystates, the bit operator bit or implementation is used for saving. The implementation code is as follows:

Public void keypressed (INT keycode ){

// Conversion button

Int key = convertkey (keycode );

// Save the button

Keystates | = key;

}

When the button is released, it is similar to pressing the button. First, convert the key value and then clear the key information. When the key is cleared, the status of the key is reversed, and then the keystates bit can be used with it. The implementation code is as follows:

Public void keyreleased (INT keycode ){

// Conversion button

Int key = convertkey (keycode );

// Clear the buttons

Keystates & = ~ Key;

}

When switching the interface, you need to clear the key status so that you only need to clear the keystates. The implementation code is as follows:

/**

* Clear buttons

*/

Private void clearkey (){

Keystates = 0;

}

The code of the actual button processing can be implemented in the 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.