Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java ME (J2ME) > 2клавиши


Автор: DEMOVERSION 5.4.2006, 16:27
как проверять состояние одновременно нажатых кнопок, например DOWN_PRESSED&RIGHT_PRESSED

Код

Код: 
int k = getKeyStates(); 
 switch (k) { 
            case FIRE_PRESSED: { 
                // 
                break; 
            } 
            case RIGHT_PRESSED:{ 
                car.addSpeed(); 
                break; 
            } 
            case LEFT_PRESSED:{ 
                car.removeSpeed(); 
                break; 
            } 
            case UP_PRESSED:{ 
                car.removePos(); 
                break; 
            } 
            case DOWN_PRESSED:{ 
                car.addPos(); 
                break; 
            } 
            case DOWN_PRESSED&RIGHT_PRESSED:{ 
                        } 
        } 

Автор: redrick 5.4.2006, 18:27
или я тебя неправильно понял или вот читай
Код

getKeyStates

public int getKeyStates()

    Gets the states of the physical game keys. Each bit in the returned integer represents a specific key on the device. A key's bit will be 1 if the key is currently down or has been pressed at least once since the last time this method was called. The bit will be 0 if the key is currently up and has not been pressed at all since the last time this method was called. This latching behavior ensures that a rapid key press and release will always be caught by the game loop, regardless of how slowly the loop runs.

    For example:

          // Get the key state and store it
          int keyState = getKeyStates();
          if ((keyState & LEFT_KEY) != 0) {
              positionX--;
          }
          else if ((keyState & RIGHT_KEY) != 0) {
              positionX++;
          }

     

    Calling this method has the side effect of clearing any latched state. Another call to getKeyStates immediately after a prior call will therefore report the system's best idea of the current state of the keys, the latched bits having been cleared by the first call.

    Some devices may not be able to query the keypad hardware directly and therefore, this method may be implemented by monitoring key press and release events instead. Thus the state reported by getKeyStates might lag the actual state of the physical keys since the timeliness of the key information is be subject to the capabilities of each device. Also, some devices may be incapable of detecting simultaneous presses of multiple keys.

    This method returns 0 unless the GameCanvas is currently visible as reported by Displayable.isShown(). Upon becoming visible, a GameCanvas will initially indicate that all keys are unpressed (0); if a key is held down while the GameCanvas is being shown, the key must be first released and then pressed in order for the key press to be reported by the GameCanvas.

    Returns:
        An integer containing the key state information (one bit per key), or 0 if the GameCanvas is not currently shown.
    See Also:
        UP_PRESSED, DOWN_PRESSED, LEFT_PRESSED, RIGHT_PRESSED, FIRE_PRESSED, GAME_A_PRESSED, GAME_B_PRESSED, GAME_C_PRESSED, GAME_D_PRESSED




итого условие будет
Код

if(((k & DOWN_PRESSED) != 0) && ((k &RIGHT_PRESSED) != 0)) {
    // нажались одновременно
}

Автор: DEMOVERSION 7.4.2006, 09:31
Спасибо!

Автор: 02077461 18.4.2006, 12:41
А в каом это объекте?..
 

Автор: redrick 18.4.2006, 13:05
конкретно это касалось GameCanvas вроде бы 

Автор: 02077461 18.4.2006, 14:07
А для Canvas как реализовать?.. 

Автор: Dancer 18.4.2006, 15:20
getKeyPressed() и getKeyReleased() есть такие методы в канве.

ну, заведи массив какой нить (можешь вообще переменную типа int использовать как бинарное представление числа и будет тебе состояние 32 кнопок)
в зависимости от того что произошло (нажал или отпустил, меняешь состояние массива (переменной). В зависимости от того каков массив (или какое значение в твоей переменной) определеляется дальнейшее действие.

Алгоритм понятен? 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)