Game Development Reference
In-Depth Information
Boxes (STBs), and so forth. These devices will also process these more widespread (in
their implementation) KeyEvent and MouseEvent types of input events, as mouse and
key events are always supported for legacy software packages.
It is interesting to note that a touchscreen display will “handle” mouse events as
well as touch events, which is very convenient as far as making sure that your game
works across as many different platforms as possible. I often use this approach of using
mouse event handling in my Android book titles, so that both the touchscreen and a
DPAD center (click) button can be used by the user to generate a mouse click event
without having to specifically use touch events. Another advantage of using mouse
(click) events where possible for touchscreen users is that if you use touch events, you
cannot go in the other direction, that is, your game application will only work on touch-
screen devices and not on devices (such as iTV sets, laptops, desktops, netbooks, and
the like) that feature mouse hardware of some type.
This same principle applies to key events, especially the arrow keys we will be us-
ing for this game, as these keys can be found on the arrow keypad on keyboards and re-
mote controls, on game controllers, and on the DPAD on most smartphones. I will also
show you how to include alternate key mapping so that your players can decide which
input method they prefer to use to play your Java 8 game. Let's take a look at the
KeyCode and KeyEvent classes next.
The KeyCode Class: Using Enum Constants to Define
Keys Players Use for Game
Since we are going to use the arrow keypad for our game, and possibly the A-S-D-W
keys, and in the future, the game controller's GAME_A , GAME_B , GAME_C and
GAME_D buttons, let's take a closer look at the KeyCode class first. This class is a
public Enum class that holds enumerated constant values. This class is where the
KeyEvent class goes to get the KeyCode constant values that it uses (processes) to de-
termine which key was used by the player for any particular key event invocation. The
Java 8 and JavaFX class hierarchy for the KeyCode class looks like the following:
java.lang.Object
> java.lang.Enum<KeyCode>
> javafx.scene.input. KeyCode
The constant values contained in the KeyCode class use capital letters , and are
named after the key that the keycode supports. For instance, the a, s, w, and d keycodes
Search WWH ::




Custom Search