Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples


The greater-than symbol is the right-facing arrowhead, and the less-than symbol is a left-facing
arrowhead. These are used before the equals sign to create greater than or equal to and less than or equal to
relational operators, respectively, as you can see at the bottom of Table 5-3.
These relational operators return a boolean value of true or false. As such, they are also used in control
(loop) structures in Java quite a bit and are used in gameplay programming logic as well to control the path
(result) that the gameplay will take. For instance, let’s say you want to determine where the left edge of the
game board is so that the GamePiece 3D object does not fall right off of the board when it is being moved to
the left. Use this relational comparison:


boolean gameBoardEdge = false; // boolean variable gameBoardEdge initialized to be
false
gameBoardEdge = (GamePieceX <= 0); // boolean gameBoardEdge set to TRUE if left side
reached


Notice that I have used <= less than or equal to (yes, Java supports negative numbers too), so that if the
GamePiece has gone past the (x=0) left side of the screen, the gameBoardEdge boolean flag will be set to
the value of true, and the game movement programming logic can deal with the situation by changing the
direction of movement (so GamePiece does not fall off GameBoard) or stopping its movement entirely
(so the GamePiece stops at the edge).
You will be getting a lot of exposure to these relational operators during this book as they are quite
useful in creating gameplay logic, so we are going to be having a lot of fun with these soon enough. Let’s take
a look at logical operators next so we can work with Boolean sets and compare things in groups, which is
also important for gaming.


Java Logical Operators: Processing Groups and Opposites


The Java logical operators are somewhat similar to the Boolean operations (union, intersection, etc.) in that
they compare Boolean values to each other and then make decisions based upon these comparisons. Java
logical operators will allow you to determine whether two Boolean variables hold the same value, which is
called an AND operation, or whether one of the Boolean variables is different from the other, which is called
an OR operation. There is also a third logical operator called the NOT operator, which will reverse the value
of any of your compared boolean operands, or even reverse the value of a boolean operand that is not being
compared, if you simply want to flip a switch or reverse a boolean flag in your gameplay programming logic.
As you may have guessed, the AND operator uses two of the AND symbols, like this: &&. The OR operator
uses two vertical bars, like this: ||. The NOT operator uses the exclamation point, like this: !. So, if I were to say
I was not joking, I would write !JOKING (hey, that would be a great programmer’s T-shirt). Table 5-4 shows
Java logical operators, with an example of each, along with a brief description.


Table 5-4. Java Logical Operators, an Example Where A=true and B=false, and a Description of the Logical
Operation


Operator Example Description:


&& (A && B) is false A logical AND operator equates to true when BOTH of the operands
hold the true value.


|| (A || B) is true A logical OR operator equates to true when EITHER of the operands
hold the true value.


! !(A && B) is true A logical NOT operator reverses the logical state of the operator (or set)
it is applied to.

Free download pdf