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 11 ■ Moving Your Action Figure in 2D: Controlling the X and Y Display Screen Coordinates<br />

The method calls that we are going to put into place during the first part of this chapter involve creating the<br />

event handlers for the Scene object, which we will do right after we have set-up that Scene object named scene.<br />

The next thing that we will need to do in the process of adding to the game is to load the Image object assets (digital<br />

image references), which is what your .loadImageAssets() method will do. Once your Image objects are declared and<br />

instantiated, we can then create the game actors using the .createGameActors() method and the constructor method<br />

calls that we created in our custom Actor and Hero subclasses, such as the Bagel.java class we created in Chapter 10.<br />

Once we have the actors created, we can add them to the Scene Graph using .addGameActorNodes() as well as adding<br />

them to the game cast using the .createCastingDirection() method. At the end, we create and start the GamePlayLoop<br />

object by calling the .createStartGameLoop() method. The .createSplashScreenNodes() and .addNodesToStackPane()<br />

are at the end, as they won’t be added to now that the splashscreen content production work has been completed. The<br />

method call code that we’ll add during this chapter looks like the following:<br />

createSceneEventHandling();<br />

loadImageAssets();<br />

createGameActors();<br />

addGameActorNodes();<br />

createCastingDirection();<br />

createSplashScreenNodes();<br />

addNodesToStackPane();<br />

createStartGameLoop();<br />

Let’s get down to business and start implementing this code redesign process for the InvinciBagel.java class.<br />

The Scene Event Handling Method: .createSceneEventHandling()<br />

The first thing that we will want to do is to move the event handling for our game play into its own method, which we<br />

will call .createSceneEventHandling(). The reason that I am creating a method for the creation of Scene object event<br />

handling is because if later on you wanted to add other types of input events into your game, such as mouse events or<br />

drag events, you will have a logical method already in place which can hold this event-related <strong>Java</strong> code.<br />

This new <strong>Java</strong> code, which can be seen in Figure 11-2, will involve taking your scene.setOnKeyPressed()<br />

and scene.setOnKeyReleased() method handling structures, created in Chapter 9, out of your<br />

.createSplashScreenNodes() method, and placing them into their own method structure. Later we’ll relocate all<br />

ActionEvent handlers, which are in the .start() method, and actually belong inside the .createSplashScreenNodes()<br />

method where they would be grouped with the other splashscreen objects. This new event handling code structure<br />

should look like the following <strong>Java</strong> code:<br />

private void createSceneEventHandling() {<br />

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

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

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

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

}<br />

});<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 />

www.it-ebooks.info<br />

231

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

Saved successfully!

Ooh no, something went wrong!