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 17 ■ Enhancing Game Play: Creating a Scoring Engine, Adding Treasure and an Enemy Auto-Attack Engine<br />

Figure 17-26. Install the skyCloud background Image (left), and use a .toBack() method call to set proper<br />

z-index (right)<br />

Since we want to have our background image at the lowest (zero) z-index so it will be behind all of our game play<br />

assets, but also have all the splashscreen assets at the highest z-index, so that those image plates will cover (be in front<br />

of) all of our game play assets, we normally would have to implement yet another ImageView compositing plate, to<br />

be able to do this. However, there is a handy set of z-index related Node class methods that will allow us to use the<br />

SplashScreenBackplate ImageView object to hold both the game splashscreen and a game background image plate at<br />

the same time! This is one of the ImageView Node optimizations that I wanted to implement to keep our game Nodes<br />

at a minimum, to reduce memory and processing overhead. The code to place the ImageView behind our game assets<br />

will call the .toBack() method off of the SplashScreenBackplate ImageView Node object, which relocates that Node<br />

to the Back (Bottom layer) of the <strong>Java</strong>FX Scene Graph Node Stack. This is the equivalent of setting this Node’s z-index<br />

to zero. The <strong>Java</strong> statement can be seen highlighted in light blue at the top of Figure 17-26, and the completed <strong>Java</strong><br />

code for your gameButton.setOnAction((ActionEvent)->{} event handling structure should look like the following:<br />

gameButton.setOnAction((ActionEvent) -> {<br />

splashScreenBackplate.setImage(skyCloud);<br />

splashScreenBackplate.setVisible(true);<br />

splashScreenBackplate.toBack();<br />

splashScreenTextArea.setVisible(false);<br />

});<br />

As you probably realize by now, we’ll need to “counter” this move in the other three Button event handling<br />

structures. We will use the opposite of the .toBack() method call, which is of course the .toFront() method call. As you<br />

can see, we need to not only call the .toFront() method off of the splashScreenBackplate ImageView Node object, but<br />

also off of the splashScreenTextArea ImageView object, and the buttonContainer HBox object that is holding our UI<br />

Button controls. We will need to call this method off of all of these Splashscreen and UI objects so that all of these are<br />

brought back to the front of the <strong>Java</strong>FX Scene Graph Node Stack. The <strong>Java</strong> code, shown in Figure 17-27, looks like this:<br />

helpButton.setOnAction((ActionEvent) -> {<br />

splashScreenBackplate.setImage(splashScreen);<br />

splashScreenBackplate.toFront();<br />

splashScreenBackplate.setVisible(true);<br />

splashScreenTextArea.setVisible(true);<br />

splashScreenTextArea.setImage(instructionLayer);<br />

splashScreenTextArea.toFront();<br />

buttonContainer.toFront();<br />

});<br />

www.it-ebooks.info<br />

415

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

Saved successfully!

Ooh no, something went wrong!