Game Development Reference
In-Depth Information
are A , S , W, and D . The arrow keypad keycodes are UP , DOWN , LEFT, and RIGHT ,
and the game controller button keycodes are GAME_A , GAME_B , GAME_C , and
GAME_D .
We will be implementing KeyCode constants along with the KeyEvent object in the
EventHandler object in a bit, after we cover these foundational packages and classes
for input event handling. As you will soon see, this is done in much the same way that
an ActionEvent is set up to be handled, and KeyEvents can be coded using the Java 7
inner class approach, or by using a Java 8 lambda expression.
We will set up our KeyEvent object handling in a very modular fashion, so that an
event KeyCode evaluation structure sets Boolean flag variables for each KeyCode
mapping. The nature of event processing is that it is a real-time engine, like the pulse
engine, so these Boolean flags will provide an accurate “view” of what keys are being
pressed or released by the player during any given nanosecond. These Boolean values
can then be read and acted upon, by using Java game programming logic in our other
game engine classes, which will then process these key events in real time.
The KeyEvent Class: Using KeyEvent Objects to Hold
KeyCode Players Are Using
Next, let's take a closer look at the KeyEvent class. This class is designated public fi-
nal KeyEvent, and it extends the InputEvent superclass, which is used to create all of
the input event subclasses that are in the javafx.scene.input package. The KeyEvent
class is set into motion using the EventHandler class, and handles KeyCode class con-
stant values. This class's hierarchy starts with the java.lang.Object master class and
goes through the java.util.EventObject event superclass to the javafx.event.Event class,
which is used to create the javafx.scene.input.InputEvent class that the KeyEvent class
extends (subclasses). It is interesting to note that we are spanning four different pack-
ages here!
The Java 8 and JavaFX class hierarchy for the KeyEvent class jumps from the
java.lang package to the java.util package to the javafx.event package to the
javafx.scene.input package. The KeyEvent class hierarchy looks like the following:
java.lang.Object
> java.util.EventObject
> javafx.event.Event
> javafx.scene.input.InputEvent
> javafx.scene.input. KeyEvent
Search WWH ::




Custom Search