28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Adding Alternate KeyEvent Mapping: Using A-S-D-W<br />

Now that we have these KeyEvent handling structures in place, let’s take a look at how easy it is to add an alternate key<br />

mapping to the ASDW keys often used for game play. This is done by adding in a few more case statements for the A,<br />

S, D, and W characters on the keyboard, and setting them to the UP, DOWN, LEFT, and RIGHT Boolean equivalents<br />

that we have set up already. This will allow users to use the A and D characters with their left hand and the UP and<br />

DOWN arrows with their right hand for easier game play, for instance.<br />

Later on, if you wanted to add more features to your game play, using your game controller, and its support for the<br />

KeyCode class’s GAME_A (Jump), GAME_B (Fly), GAME_C (climb), and GAME_D (crawl) constants, all that you would<br />

have to do is to add these new features into your game would be to add another four Boolean variables (jump, fly, climb,<br />

and crawl) to the up, down, left, and right at the top of the screen, and add in another four case statements.<br />

These four W (UP), S (DOWN), A (LEFT), and D (RIGHT) case statements, once added to the switch statement,<br />

would bring your KeyEvent object and its event handling <strong>Java</strong> code structure to only a dozen lines of <strong>Java</strong> code. Your<br />

new .setOnKeyPressed() event handling structure would look like this block of code after you make this modification:<br />

scene.setOnKeyPressed(KeyEvent event) -> {<br />

switch (event.getCode()) {<br />

case UP: up = true; break;<br />

case DOWN: down = true; break;<br />

case LEFT: left = true; break;<br />

case RIGHT: right = true; break;<br />

case W: up = true; break;<br />

case S: down = true; break;<br />

case A: left = true; break;<br />

case D: right = true; break;<br />

}<br />

});<br />

As you can see, now the user can use either set of keys, or both sets of keys at the same time, to control the game<br />

play. Now that you have made the .setOnKeyPressed() event handling structure more flexible (and powerful) for the<br />

game player, let’s do the same thing to the .setOnKeyReleased() event handling structure, which will instead set a<br />

false value to the up, down, left and right Boolean flag variables when the user has released the A or LEFT, W or UP,<br />

S or DOWN, or D or RIGHT keys on the keyboard, remote control, or device keyboard and keypad.<br />

Your .setOnKeyReleased() event handling <strong>Java</strong> code should look like the following once you add these case<br />

statements at the end of the body of the switch statement:<br />

scene.setOnKeyReleased(KeyEvent event) -> {<br />

switch (event.getCode()) {<br />

case UP: up = false; break;<br />

case DOWN: down = false; break;<br />

case LEFT: left = false; break;<br />

case RIGHT: right = false; break;<br />

case W: up = false; break;<br />

case S: down = false; break;<br />

case A: left = false; break;<br />

case D: right = false; break;<br />

}<br />

});<br />

Now that you have added another set of player movement control keys for your player to use to control the game<br />

play, your code is error free, and has a simple, effective structure, as is shown in Figure 9-14. We are handling the<br />

events one time at the very top of the Scene object named scene, not involving any Scene Graph Node objects in this<br />

event handling “calculation,” and are using only a few bytes of memory to hold eight Boolean (on/off) values.<br />

204<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!