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 />

currentlyMoving (a new object will not be moving when it appears, even if that is only for a fraction of a second)<br />

variables, which you will initialize, as you did for the default constructor. The other five variables (states) will be set using<br />

parameters passed in via a parameter list, using an equals assignment operator. This is done using the following code:<br />

GamePiece(int x, int y, String orientation, int lifespan, String direction, String movement) {<br />

invinciBagelX = x;<br />

invinciBagelY = y;<br />

bagelOrientation = orientation;<br />

lifeIndex = lifespan;<br />

hitsIndex = 0;<br />

directionFacing = direction;<br />

movementType = movement;<br />

currentlyMoving = false;<br />

}<br />

I bolded the variables in the parameter list, as well as where they are used inside the constructor method, to set the<br />

states (variables) for the object. These variables are declared at the top of the GamePiece class, which you have used<br />

to design, define, and create the GamePiece object. This second constructor method can be said to overload the first<br />

constructor method, because it uses the exact same method call (method name), with a different parameter list (full of<br />

parameters, versus empty or no parameters). This gives you the default object constructor method as well as a custom<br />

object constructor method, so in your game logic, you can create a default GamePiece or a custom GamePiece.<br />

Summary<br />

In this third chapter you took a look at some of the more important concepts and structures found in the <strong>Java</strong> 8<br />

programming language. Certainly, I cannot cover everything in <strong>Java</strong> in one chapter, so I stuck with concepts, constructs,<br />

and keywords that you will be using to create a game over the course of this book. Most <strong>Java</strong> books are 800 pages or more,<br />

so if you want to get really deep into <strong>Java</strong>, I suggest <strong>Beginning</strong> <strong>Java</strong> 8 Fundamentals by Kishori Sharan (Apress, 2014).<br />

You started by taking a high-level view of <strong>Java</strong>, considering its syntax, including <strong>Java</strong> comments and delimiters,<br />

APIs, and the <strong>Java</strong> packages that a <strong>Java</strong> API contains. You also studied <strong>Java</strong> classes, including nested classes and<br />

inner classes, as the <strong>Java</strong> packages contain <strong>Java</strong> classes. You then went the next level down in <strong>Java</strong>, to the method,<br />

which is like the function in other programming languages, as well as to a special kind of <strong>Java</strong> method called a<br />

constructor method.<br />

Next, you explored how <strong>Java</strong> represents data, using fields, or data fields, examining the different types, such as<br />

constants, or fixed data fields, and variables, or data fields that can change their values. After that, you took a closer<br />

look at <strong>Java</strong> modifier keywords, including the public, private, and protected access control keywords and the final,<br />

static, abstract, volatile, and synchronized nonaccess control modifier keywords.<br />

After finishing with the basic code structures and how to modify them, you moved on to the primary <strong>Java</strong> data<br />

types, such as boolean, char, byte, int, float, short, long, and double and then explored the <strong>Java</strong> operators that are<br />

used to process, or bridge, these data types over to your programming logic. You studied arithmetic operators, for use<br />

with numeric values; logical operators, for use with boolean values; relational operators, to consider relationships<br />

between data values; conditional operators, which allow you to establish any conditional variable assignments; and<br />

assignment operators, which let you assign values to (or between) variables.<br />

Then, you looked at <strong>Java</strong> logic control structures, including decision-making control structures (I like to call<br />

them decision trees) and looping, or iterative, logic control structures. You learned about the <strong>Java</strong> switch-case<br />

structure, the if-else structure, the for loop structure, and the do-while loop structures. Finally, you examined<br />

<strong>Java</strong> objects and discovered how to define object attributes, states, and behaviors, using a <strong>Java</strong> class, methods, and<br />

constructor methods.<br />

In the next chapter, I will give you an overview of the <strong>Java</strong>FX multimedia engine, and its classes and capabilities,<br />

as you will be leveraging <strong>Java</strong>FX to add media elements to your games, such as images, video, and audio, and to<br />

control your games, using <strong>Java</strong>FX object constructs (classes), such as the Stage, Scene, and StackPane.<br />

74<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!