8
Gaming API and Sound
•public static final int VOLUME_MIN - The minimum volume for playing sound effects. This constant has a value of 0.
GameScreen Methods
The GameScreen class defines the following methods:
•protected Graphics getGraphics() - Obtains the Graphics object for rendering GameScreens. The Graphics object renders to an
•public int getKeyStates() - Gets the states of the physical keys. Each bit in the returned integer represents a specific key on the device. A key's bit will be set if the key is currently pressed or was pressed at least once since the last time this method was called. The bit will be 0 if the key is not currently pressed and was not 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. This method may be called twice to check if a key is currently pressed; that is, calling this method twice effectively disables the latching behavior. The lower bits are defined by UP_KEY, DOWN_KEY, LEFT_KEY, etc.; the remaining bits may be mapped to
The following is a code sample to show implementation of public int getKeyStates():
Public int getKeyStates ()
// Get the key state and store it
int keyState = gameScreenObject.getKeyStates(); if ((keyState & LEFT_KEY) != 0) {
} else if ((keyState & RIGHT_KEY) != 0) { positionX++;
}
•public void enableKeyEvents(boolean enabled) - Enables or disables key event calls to this GameScreen. If disabled, the Canvas key event methods (keyPressed, keyRepeated, keyReleased) are not called when keys are pressed or released; however, the developer can still call getKeyStates to query the state of the keys. For games that poll key state and do not need
41