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

While we are adding game design elements to the game, let’s go ahead and learn how to use a splashscreen<br />

ImageView Node object to implement a game background image plate. We will do this so that our white iBeagle, and<br />

(Cream) iCheese and iBullet objects, stand out to the game player better, using an enhanced blue background color.<br />

Adding a Background Image: Using .toBack( ) Method<br />

Before we get into coding the collision and scoring routines for the Enemy and Projectile objects, let’s add an Image<br />

asset (object) to use in the background plate, while we are in the process of adding objects to our game. Copy the<br />

skycloud.png 8-bit PNG8 image asset, seen in the bottom-left corner of Figure 17-24, from the book repository into<br />

the NetBeansProjects/InvinciBagel/src folder. After you do this, add a skyCloud Image object declaration to the end of<br />

the private Image object declarations for the splashscreen related image assets, as seen at the top of Figure 17-24. As<br />

you can see, there will be a warning highlight under this object until you implement (use) it in your <strong>Java</strong> code. Next,<br />

instantiate a skyCloud object in .loadImageAssets(), as seen at the bottom of Figure 17-24, using the following code:<br />

private Image splashScreen, instructionLayer, legalLayer, scoresLayer, skyCloud;<br />

skyCloud = new Image("/skycloud.png", 640, 400, true, false, true);<br />

Figure 17-24. Declare and instantiate a skyCloud Image object that references a skycloud.png background image asset<br />

Now you have a skyCloud object, so use a .setImage() method to set a SplashScreenBackground ImageView<br />

Node, to use this image asset inside the gameButton.setOnAction(ActionEvent) code block, so that when you click the<br />

PLAY GAME Button, this image is set as the backplate image. Also, make sure to use the .setVisible() method call with<br />

a value of true, so that the ImageView Node will be visible. The <strong>Java</strong> code, shown in Figure 17-25, should look like this:<br />

splashScreenBackground.setImage(skyCloud);<br />

splashScreenBackground.setVisible(true);<br />

www.it-ebooks.info<br />

413

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

Saved successfully!

Ooh no, something went wrong!