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

Figure 9-14. Add the case statements for ASDW keys to give users two key options, or to allow two-handed game play<br />

This is in keeping with our objective of optimizing both memory and CPU cycles, so that these are available for<br />

the more advanced parts of our game play, such as the game play logic, collision detection, or physics calculation.<br />

We also added constants that will allow us to later scale this 640 by 400 game prototype to fit display screens of<br />

different resolutions such as Pseudo HD (1280 by 720), True HD (1920 by 1080), and UHD (4096 by 2160). These can<br />

also be used in the game logic, to calculate the size of the screen area to determine movement boundaries.<br />

So far, we have added our actor and supporting cast engine, as well as our basic event handling processing, so<br />

that we can start to determine how this InvinciBagel game hero is going to move around on the screen in the next<br />

chapter. We have .handle() as well as .update() and .collide() methods in place to hold the code, which will animate a<br />

character, and eventually enemies, in both the single-player as well as a future multi-player version of this game.<br />

Next, let’s revisit our overview diagram of this game design, and take a look at the InvinciBagel package, the<br />

InvinciBagel.java class, and the GamePlayLoop and Actor and Hero classes, which provide the foundation for our<br />

game play processing and actor (and projectiles, treasure, enemy and obstacles or “props”) creation.<br />

Updating Our Game Design: Adding Event Handling<br />

Let’s update the diagram that I introduced in Chapter 7 (Figure 7-19) and updated in Chapter 8 (Figure 8-17) to<br />

include the ActionEvent and KeyEvent handling by the EventHandler class. As you can see in Figure 9-15, I added<br />

the EventHandler event handling class to the diagram as well as the ActionEvent objects that handle our UI design<br />

control and the KeyEvent that we are going to use to move the InvinciBagel actor around the screen. Since the<br />

.setOnKeyPressed() and .setOnKeyReleased() methods are called off of the scene Scene object named scene, and the<br />

ActionEvent is also contained under the Scene object, I placed these inside of the Scene object in the diagram.<br />

www.it-ebooks.info<br />

205

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

Saved successfully!

Ooh no, something went wrong!