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 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />

public void setInvinciBagelHitsIndex(int damage) {<br />

hitsIndex = damage;<br />

}<br />

public String getInvinciBagelMovementType() {<br />

return movementType;<br />

}<br />

// Set Method for Hits (damage)<br />

// Get Method for Movement Type<br />

}<br />

public void setInvinciBagelMovementType(String movement) {<br />

movementType = movement;<br />

}<br />

// Set Method for Movement Type<br />

It is important to note that these constants, variables, and methods are for demonstration of how the class,<br />

method, and data field keywords let developers create (virtualize) their game components. As you develop the game,<br />

these will probably change, as game development is a process of refinement, in which you will be constantly changing<br />

and enhancing the <strong>Java</strong> code base to add features and capabilities.<br />

Now, all you have to do is add your GamePiece() constructor method, which will create a new object with<br />

the initialized variable settings that you want the default GamePiece to contain. Then, you will create the second<br />

overloaded constructor method. This second constructor method will allow parameters to be passed into a<br />

constructor method so that you can provide custom (nondefault) settings to these same variables (states). In this way,<br />

if you call GamePiece(), you get a default object; if you call GamePiece(parameter list here), you get a custom object.<br />

Creating a GamePiece() Constructor: Overloading a GamePiece<br />

Finally, let’s create the constructor method (two, actually), which takes the states (variables) from the GamePiece class<br />

and creates a default object. You will use this object to create the custom overloaded constructor method. The first<br />

constructor method will employ the package private access control method, using no access modifier keyword, so<br />

that any code in the invincibagel package can call this constructor method. Then, you will set your default variables,<br />

using the following <strong>Java</strong> code:<br />

GamePiece() {<br />

invinciBagelX = 0;<br />

invinciBagelY = 0;<br />

bagelOrientation = "side";<br />

lifeIndex = 1000;<br />

hitsIndex = 0;<br />

directionFacing = "E";<br />

movementType = "idle";<br />

currentlyMoving = false;<br />

}<br />

The overloaded constructor method will have parameters declared in the method parameter list area for those<br />

variables that are logical to allow variations for upon object creation. The only two that are not logical to allow<br />

variations for are hitsIndex (a new object will not have sustained any damage points and will thus need to be 0) and<br />

www.it-ebooks.info<br />

73

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

Saved successfully!

Ooh no, something went wrong!