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

What the iBagel.update() <strong>Java</strong> statement does is to call the .update() method for the Bagel object named iBagel<br />

on every pulse event. Anything that you put into this .update() method will be executed 60 times every second. Any<br />

other Actor object that you want processed at 60 FPS, simply add a similar .update() call to this .handle() method.<br />

Moving the iBagel Actor Object: Coding Your .update() Method<br />

Now we are ready to start developing the code that will move our InvinciBagel Actor object around the screen. We<br />

will be refining this <strong>Java</strong> code during the remainder of the book, as everything revolves around this primary Actor<br />

object, and his movement. This includes where he moves (boundaries and collisions), how fast he moves (speed and<br />

physics), and what he looks like when he moves (animating between sprite image cels or “states”). All of this code will<br />

originate inside of the iBagel object’s .update() method, and so we are going to start this lengthy journey by adding<br />

some basic code that looks at the Boolean variables that are in our InvinciBagel.java class, and which hold the arrow<br />

(or ASDW keys) key pressed and released states, and then process these states using conditional If statements. The<br />

results of this conditional statement processing will then move the InvinciBagel character on the screen (initially,<br />

later we will add more advanced programming logic). We will eventually make this movement and interaction more<br />

and more “intelligent.” The first thing that we will want to do is to make the Boolean variables for up, down, left, and<br />

right visible to the Bagel class using import static statements, as we did earlier in the chapter, to make the iBagel object<br />

visible to the GamePlayLoop class .handle() method. The four added import static statements will look like this:<br />

package invincibagel;<br />

import static invincibagel.InvinciBagel.down;<br />

import static invincibagel.InvinciBagel.left;<br />

import static invincibagel.InvinciBagel.right;<br />

import static invincibagel.InvinciBagel.up;<br />

import javafx.scene.image.Image;<br />

public class Bagel extends Hero {...}<br />

As you can see in Figure 11-12, there are no error or warning highlights regarding this code, and we’re ready to<br />

move on and add the conditional programming logic that will look at which of these four variables are set to true, or<br />

KeyPressed, and which are set to false, or KeyReleased. Inside of these conditional statements we’ll place the code<br />

that will move the iX and iY (Actor location) variables, based on the vX and vY (Actor velocity of movement) variables.<br />

Figure 11-12. Add import static invincibagel.InvinciBagel references to static boolean down, left, right, and up<br />

variables<br />

www.it-ebooks.info<br />

243

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

Saved successfully!

Ooh no, something went wrong!