Game Development Reference
In-Depth Information
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
games.
Java Logical Operators:
The Java logical operators are similar to the boolean operations (union, intersection,
etc.) that you learned about in school, and allow you to determine if both boolean vari-
ables hold the same value ( AND ), or if one of the boolean variables is different ( OR ),
from the other. There's also a NOT operator that reverses the value of any of the com-
pared boolean operands. Table 3-4 shows Java's three logical operators, and an ex-
ample of each, along with a description.
Table 3-4 . Java Logical Operators, an Example in Which A = True and B = False, and
a Description of Logical Operation
Operator Example
Description
&&
(A && B) is
false
A logical AND operator equates to true when BOTH operands
are the same value.
||
(A || B) is
true
A logical OR operator equates to true when EITHER operand is
the same value.
!
!(A && B)
is true
A logical NOT operator reverses the logical state of the operator
(or set) it is applied to.
Let's use logical operators to enhance the game logic example in the previous sec-
tion by including the direction in which the InvinciBagel is moving on the screen. The
existing facingDirection String variable will control the direction the InvinciBagel is
facing (and moving in, if in motion). You can now use the following logical operator to
determine if the InvinciBagel is facing left (W, or W est); if the travelingWest boolean
variable is true ; AND if the hit (or passed) boolean variable on the left-hand side of the
screen, hitLeftSideScrn , is also equal to true . The modified code for doing this will
include two more boolean variable declarations and initializations and will look like
this:
boolean changeDirection = false ; // Create boolean
variable changeDirection, initialize to false
boolean hitLeftSideScrn = false ; // Create boolean
variable hitLeftSideScrn, initialize to false
 
 
Search WWH ::




Custom Search