28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 ■ Controlling Your Action Figure: Implementing <strong>Java</strong> Event Handlers and Using Lambda Expressions<br />

It is interesting to note that a touchscreen display will “handle” mouse events as well as touch events, which is<br />

very convenient as far as making sure that your game works across as many different platforms as possible. I often<br />

use this approach of using mouse event handling in my Android book titles, so that both the touchscreen and a DPAD<br />

center (click) button can be used by the user to generate a mouse click event without having to specifically use touch<br />

events. Another advantage of using mouse (click) events where possible for touchscreen users is that if you use touch<br />

events, you cannot go in the other direction, that is, your game application will only work on touchscreen devices and<br />

not on devices (such as iTV sets, laptops, desktops, netbooks, and the like) that feature mouse hardware of some type.<br />

This same principle applies to key events, especially the arrow keys we will be using for this game, as these keys<br />

can be found on the arrow keypad on keyboards and remote controls, on game controllers, and on the DPAD on most<br />

smartphones. I will also show you how to include alternate key mapping so that your players can decide which input<br />

method they prefer to use to play your <strong>Java</strong> 8 game. Let’s take a look at the KeyCode and KeyEvent classes next.<br />

The KeyCode Class: Using Enum Constants to Define Keys Players Use for Game<br />

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<br />

controller’s GAME_A, GAME_B, GAME_C and GAME_D buttons, let’s take a closer look at the KeyCode class first.<br />

This class is a public Enum class that holds enumerated constant values. This class is where the KeyEvent class goes<br />

to get the KeyCode constant values that it uses (processes) to determine which key was used by the player for any<br />

particular key event invocation. The <strong>Java</strong> 8 and <strong>Java</strong>FX class hierarchy for the KeyCode class looks like the following:<br />

java.lang.Object<br />

> java.lang.Enum<br />

> javafx.scene.input.KeyCode<br />

The constant values contained in the KeyCode class use capital letters, and are named after the key that the<br />

keycode supports. For instance, the a, s, w, and d keycodes are A, S, W, and D. The arrow keypad keycodes are<br />

UP, DOWN, LEFT, and RIGHT, and the game controller button keycodes are GAME_A, GAME_B, GAME_C, and<br />

GAME_D.<br />

We will be implementing KeyCode constants along with the KeyEvent object in the EventHandler object in a bit,<br />

after we cover these foundational packages and classes for input event handling. As you will soon see, this is done in<br />

much the same way that an ActionEvent is set up to be handled, and KeyEvents can be coded using the <strong>Java</strong> 7 inner<br />

class approach, or by using a <strong>Java</strong> 8 lambda expression.<br />

We will set up our KeyEvent object handling in a very modular fashion, so that an event KeyCode evaluation<br />

structure sets Boolean flag variables for each KeyCode mapping. The nature of event processing is that it is a real-time<br />

engine, like the pulse engine, so these Boolean flags will provide an accurate “view” of what keys are being pressed or<br />

released by the player during any given nanosecond. These Boolean values can then be read and acted upon, by using<br />

<strong>Java</strong> game programming logic in our other game engine classes, which will then process these key events in real time.<br />

The KeyEvent Class: Using KeyEvent Objects to Hold KeyCode Players Are Using<br />

Next, let’s take a closer look at the KeyEvent class. This class is designated public final KeyEvent, and it extends<br />

the InputEvent superclass, which is used to create all of the input event subclasses that are in the javafx.scene.<br />

input package. The KeyEvent class is set into motion using the EventHandler class, and handles KeyCode class<br />

constant values. This class’s hierarchy starts with the java.lang.Object master class and goes through the java.<br />

util.EventObject event superclass to the javafx.event.Event class, which is used to create the javafx.scene.input.<br />

InputEvent class that the KeyEvent class extends (subclasses). It is interesting to note that we are spanning four<br />

different packages here!<br />

www.it-ebooks.info<br />

197

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!