Game Development Reference
In-Depth Information
Creating a GamePiece() Constructor: Overloading a
GamePiece
Finally, let's create the constructor method (two, actually), which takes the states (vari-
ables) from the GamePiece class and creates a default object. You will use this object
to create the custom overloaded constructor method. The first constructor method will
employ the package private access control method, using no access modifier keyword,
so that any code in the invincibagel package can call this constructor method. Then,
you will set your default variables, using the following Java code:
GamePiece() {
invinciBagelX = 0;
invinciBagelY = 0;
bagelOrientation = "side";
lifeIndex = 1000;
hitsIndex = 0;
directionFacing = "E";
movementType = "idle";
currentlyMoving = false;
}
The overloaded constructor method will have parameters declared in the method
parameter list area for those variables that are logical to allow variations for upon ob-
ject creation. The only two that are not logical to allow variations for are hitsIndex (a
new object will not have sustained any damage points and will thus need to be 0) and
currentlyMoving (a new object will not be moving when it appears, even if that is only
for a fraction of a second) variables, which you will initialize, as you did for the default
constructor. The other five variables (states) will be set using parameters passed in via
a parameter list, using an equals assignment operator. This is done using the following
code:
GamePiece(int x , int y , String orientation , int lifespan ,
String direction , String movement ) {
invinciBagelX = x ;
invinciBagelY = y ;
bagelOrientation = orientation ;
lifeIndex = lifespan ;
hitsIndex = 0;
Search WWH ::




Custom Search