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

Now that we have put our Image assets into place, created our Actor object, added him to the Scene Graph,<br />

created a CastingDirector engine, and added our iBagel to the cast, we’re ready to deal with the Game Timing Engine.<br />

Create and Start Your GamePlayLoop: .createStartGameLoop<br />

I am going to jump ahead of the .createSceneGraphNodes() method, which is still our most complex method body<br />

and which I am saving for last, and create a new method called .createStartGameLoop(). Inside of this method<br />

we’re going to create, and then start, our GamePlayLoop object, which we created back in Chapter 7 using the<br />

GamePlayLoop.java class. This class extends the <strong>Java</strong>FX AnimationTimer superclass to provide access to the <strong>Java</strong>FX<br />

pulse timing engine for our game. Inside of the .createStartGameLoop() method we are going to use the <strong>Java</strong> new<br />

keyword to create a pulse engine for our game named gamePlayLoop using the GamePlayLoop() constructor method.<br />

After that, we are going to call the .start() method off of this gamePlayLoop object to start the pulse event timing<br />

engine. This call is done by using the following four lines of <strong>Java</strong> programming logic, which are also shown error-free<br />

in Figure 11-9:<br />

private void createStartGameLoop() {<br />

gamePlayLoop = new GamePlayLoop();<br />

gamePlayLoop.start();<br />

}<br />

Figure 11-9. Create a private void createStartGameLoop() method, and create and .start() the gamePlayLoop object<br />

As you can see at the bottom of Figure 11-9, I have collapsed the other method structures, and I have them in<br />

the same order in the code that they are called within the .start() method, for organization purposes. I start a game<br />

play loop last because I want to make sure I have done absolutely everything else that I need to do to set up the game<br />

environment first, before I start the <strong>Java</strong>FX pulse engine firing and launch the game. As you can see, I’m using the <strong>Java</strong><br />

method names, and my game code design, to keep me reminded about what I need to do every time that I add some<br />

new game actor, which now that our hero is in place, could be game props, projectiles, enemies, treasure, and so on.<br />

239

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

Saved successfully!

Ooh no, something went wrong!