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 13 ■ Animating Your Action Figure States: Setting the Image States Based on KeyEvent Processing<br />

Figure 13-13. Add if() statement logic to exclude processing of nested if() hierarchy if down/up keys pressed to isRight()<br />

Next, let’s make the ASDW keys their own sprite control keys, instead of having them mimic the arrow keys.<br />

Adding Event Handling: Giving ASDW Keys Function<br />

Since I have a few more sprite states that I want to implement, I’m going to upgrade the event handler to use a twohanded<br />

play scenario, using arrow keys with ASDW, or ABCD (color) game controller buttons, using this code upgrade:<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: wKey = true; break;<br />

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

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

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

}<br />

www.it-ebooks.info<br />

289

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

Saved successfully!

Ooh no, something went wrong!