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 8 ■ Creating Your Actor Engine: Design the Characters for Your Game and Define Their Capabilities<br />

As you can see in Figures 8-13 and 8-14, we did the same type of compound statement for the initializations:<br />

lifeSpan = 1000;<br />

vX = vY = damage = offsetX = offsetY = 0;<br />

boundScale = boundRot = friction = gravity = bounce = 0;<br />

Figure 8-14. Optimize your <strong>Java</strong> code by using compound declarations, and leveraging default initialization values<br />

This could also be done using only two lines of code, if you happen to be editing on an HDTV display screen:<br />

lifeSpan = 1000;<br />

vX = vY = damage = offsetX = offsetY = boundScale = boundRot = friction = gravity = bounce = 0;<br />

Next, if we depend on the <strong>Java</strong> compiler to initialize our variables to zero, as double and float variables will be<br />

initialized to if no initialization value has been specified, we can reduce these two lines of code to one line of code:<br />

lifeSpan = 1000;<br />

Now that we’ve finished the “core” of our Hero() constructor method, let’s have NetBeans write some code!<br />

Accessing Hero Variables: Creating Getter and Setter Methods<br />

Create a line of space after your .collide() method, and place your cursor there, which will show NetBeans where<br />

you want it to place the code that it is about to generate. This is shown in Figure 8-15 by a light-blue shaded line seen<br />

behind the Source menu. Use a Source > Insert Code menu sequence or the Alt-Insert keystroke combination, and<br />

when the Generate floating pop-up menu appears under this blue line (this shows the selected line of code), select<br />

the Getter and Setter option, shown highlighted in Figure 8-15, and select all of the Hero classes. Make sure that all of<br />

the Hero class variables are selected, either by using the Hero class master selection check box, or by using the check<br />

box UI elements for each variable, as is shown on the right hand side of Figure 8-15.<br />

182<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!