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

Controlling Your Action Figure:<br />

Implementing <strong>Java</strong> Event Handlers<br />

and Using Lambda Expressions<br />

Now that we have created the public abstract Actor and Hero classes, which I call the “actor engine,” in Chapter 8,<br />

let’s get back into our InvinciBagel.java primary application class coding here in Chapter 9, and create the event<br />

handling framework that we can use to control the primary hero for our game, the InvinciBagel himself. The event<br />

handling that implements the interface between the player and your game programming logic could be looked at<br />

as the “interactivity engine” for your game, if we follow the engine paradigm we have been using thus far. There<br />

are many ways to interface with a game, including arrow keys (also known as a DPAD for consumer electronics<br />

devices), the keyboard, a mouse or trackball, a game controller, touchscreen, or even advanced hardware,<br />

such as gyroscopes and accelerometers. One of the choices you will make for your game development will be<br />

how a player will interface with the game, using the hardware device they are playing the game on, and the input<br />

capabilities it supports.<br />

During this chapter, we will be doing a number of upgrades to your InvinciBagel.java class. The first is to add<br />

support for the game WIDTH and HEIGHT variables in the form of <strong>Java</strong> constants. These will allow us to change the<br />

width and height of the game play surface, which is the area inside of the game window that pops up, or the entire<br />

screen, if your game player is using a consumer electronics device.<br />

The second upgrade we will do is to add the <strong>Java</strong> code that will create a blank white screen for us to design the<br />

game over (on top of) during the next several chapters. We’ll do this by installing a Color.WHITE background color<br />

in the Scene() constructor method call (along with our new width and height variables), and then installing <strong>Java</strong> code<br />

into our Button control event handler structure that are already in place, to hide the two ImageView “plates” we are<br />

using for image compositing of our splash screen UI design. We can also use these two ImageView Node objects later<br />

on to hold background imagery for our game play, once we get into that level of design. Remember it is important to<br />

keep the number of Nodes in the Scene Graph to a minimum, so we will reuse Node objects, instead of adding more.<br />

The third upgrade we’ll add is to add keyboard event handling routines to our Scene object that will handle the<br />

arrow key support that we will use for the game, to span any hardware device that has an arrow key pad or DPAD. This<br />

will handle any events at the top-level of the Scene down to the StackPane (Scene Graph) hierarchy. This will pass the<br />

arrow key values pressed by the user to our Node objects. This will eventually allow motion control code to move the<br />

actors around the game, which is something we will be getting involved with in greater detail in the next chapter.<br />

Besides upgrading our InvinciBagel.java code, and adding keyboard event handling, we will also be learning<br />

about lambda expressions during this chapter, just to make sure I get everything that is new in <strong>Java</strong> 8 covered during<br />

this book. These lambda expressions are somewhat advanced to be covered in this beginner level book, but since they<br />

are a major new feature of <strong>Java</strong> 8, and provide multi-threading support as well as more compact code structures, I am<br />

going to cover them here in this chapter, partly because NetBeans 8 (no surprise here) is willing to code them for you!<br />

www.it-ebooks.info<br />

187

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

Saved successfully!

Ooh no, something went wrong!