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 7 ■ The Foundation of Game Play Loop: The <strong>Java</strong>FX Pulse System and the Game Processing Architecture<br />

Figure 7-19. Current invincibagel package, class, and object hierarchy, after addition of the GamePlayLoop<br />

Next, , before moving on to the GamePlayLoop AnimationTimer class and object, you are going to place some<br />

relatively simple <strong>Java</strong> code inside the currently empty .handle() method. You will do this to make sure that the pulse<br />

engine is processing and to see just how fast 60FPS is! (I have to admit, my curiosity is getting the best of me!).<br />

Testing the GamePlayLoop: Animating the UI Container<br />

Let’s move one of the existing Scene Graph nodes, for example, the HBox layout container parent (branch) node,<br />

which contains the four UI Button control elements, counterclockwise around the InvinciBagel splash screen. You will<br />

do this by using a simple if-else <strong>Java</strong> loop control programming structure to read (using a .get() method) and control<br />

(using a .set() method) Pos constants that control (in this case) the corner of the screen placement.<br />

First, declare a Pos object named location at the top of the GamePlayLoop class. Then, click on the error message<br />

highlighting, press Alt+Enter, and select the Import Pos class option so that NetBeans will write your import<br />

statement for you. Next, inside the .handle() method, add an if-else conditional statement that evaluates this Pos<br />

object named location and compares it with the four Pos class constants that represent the four corners of the display<br />

screen, including BOTTOM_LEFT, BOTTOM_RIGHT, TOP_RIGHT, and TOP_LEFT. Your <strong>Java</strong> code should look like<br />

the following if-else conditional statement <strong>Java</strong> program structure (see also Figure 7-20):<br />

Pos location;<br />

@Override<br />

public void handle(long now) {<br />

location = InvinciBagel.buttonContainer.getAlignment();<br />

if (location == Pos.BOTTOM_LEFT) {<br />

InvinciBagel.buttonContainer.setAlignment(Pos.BOTTOM_RIGHT);<br />

} else if (location == Pos.BOTTOM_RIGHT) {<br />

InvinciBagel.buttonContainer.setAlignment(Pos.TOP_RIGHT);<br />

} else if (location == Pos.TOP_RIGHT) {<br />

InvinciBagel.buttonContainer.setAlignment(Pos.TOP_LEFT);<br />

} else if (location == Pos.TOP_LEFT) {<br />

InvinciBagel.buttonContainer.setAlignment(Pos.BOTTOM_LEFT);<br />

}<br />

}<br />

160<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!