Game Development Reference
In-Depth Information
Figure 8-14 . Optimize your Java code by using compound declarations, and leveraging default initialization values
This could also be done using only two lines of code, if you happen to be editing on
an HDTV display screen:
lifeSpan = 1000;
vX = vY = damage = offsetX = offsetY = boundScale
= boundRot = friction = gravity = bounce = 0;
Next, if we depend on the Java compiler to initialize our variables to zero, as
double and float variables will be initialized to if no initialization value has been speci-
fied, we can reduce these two lines of code to one line of code:
lifeSpan = 1000;
Now that we've finished the “core” of our Hero() constructor method, let's have
NetBeans write some code!
Accessing Hero Variables: Creating Getter and Setter
Methods
Create a line of space after your .collide() method, and place your cursor there, which
will show NetBeans where 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 behind the Source menu.
Use a Source > Insert Code menu sequence or the Alt-Insert keystroke combination,
and when the Generate floating pop-up menu appears under this blue line (this shows
the selected line of code), select the Getter and Setter option, shown highlighted in
Figure 8-15 , and select all of the Hero classes. Make sure that all of the Hero class vari-
ables are selected, either by using the Hero class master selection check box, or by us-
ing the check box UI elements for each variable, as is shown on the right hand side of
Figure 8-15 .
 
Search WWH ::




Custom Search