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

The remaining methods in the Actor class will be what are commonly termed “getter” and “setter" methods<br />

because these methods provide access to the data variables inside of the class. Using getter and setter methods is the<br />

correct way to do things, because doing this implements the <strong>Java</strong> concept (and advantage) of encapsulation, which<br />

allows <strong>Java</strong> objects to be self-contained vessels of object attributes (variable data values) and behaviors (methods).<br />

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

One of the really powerful (and time saving, as you are about to see) features of NetBeans is that it will write all of your<br />

.get() and .set() methods for each of your object and data variables for you automatically. We will be using this handy<br />

feature during this book whenever possible, so you can get used to using this time saving feature to write lots of <strong>Java</strong><br />

8 code for you, accelerating your <strong>Java</strong> 8 game code production output! You access this auto-coding feature by using<br />

the Source menu, and its Insert Code submenu, as can be seen in Figure 8-9. As you can see, there is also a keyboard<br />

short-cut (Alt-Insert); using either of these will bring up a floating Generate menu, which is shown highlighted in red<br />

in the bottom center of Figure 8-9.<br />

Figure 8-9. Use Source ➤ Insert Code menu (or Alt+Insert) to bring up a Generate Getter and Setter dialog and select all<br />

Click on the Getter and Setter option, shown highlighted in the middle of the Generate floating menu, and a<br />

Generate Getters and Setters dialog will appear, which is shown on the right side of Figure 8-9. Make sure that the<br />

hierarchy is open, and that the check box next to Actor is selected, which will automatically select all of the variables<br />

inside of the class, in this case, a dozen variables also shown selected on the right hand side of Figure 8-9.<br />

Once all of these are selected, click on the Generate button at the bottom of the dialog and generate the<br />

24 .get() and .set() methods that you would have to type in manually if NetBeans 8.0 did not offer this handy<br />

IDE feature.<br />

174<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!