Game Development Reference
In-Depth Information
boolean
travelingWest =
false
; // Create boolean
variable travelingWest,
initialize
to false
hitLeftSideScrn =
(invinciBagelX <= 0)
; // boolean
hitLeftSideScrn is
TRUE
if left side reached
travelingWest =
(facingDirection == "W")
// boolean
travelingWest is
TRUE
if facingDirection="
W
"
changeDirection =
(hitLeftSideScrn
&&
travelingWest)
//
Change Direction, if
both
equate to
TRUE
To find out if the InvinciBagel is facing (or traveling, if also moving)
West
, you
create another
travelingWest
boolean variable and initialize it (set it equal) to
false
(because your initial facingDirection setting is East). Then, you create a boolean vari-
able called
hitLeftSideScrn
, setting that to the
(invinciBagelX <= 0)
relational operat-
or statement.
Finally, you create a relational operator statement with the
travelingWest =
(facingDirection == "W")
logic, and then you are ready to use the
changeDirection
boolean variable with your new logical operator. This logical operat-
or will make sure that
both
the
hitLeftSideScrn
and
travelingWest
boolean variables
are set to
true
, using the
changeDirection = (hitLeftSideScrn &&
travelingWest)
logical operation programming statement.
Now, you have a little practice declaring and initializing variables and using rela-
tional and logical operators to determine the direction and location of a primary game
Next, let's take a look at assignment operators.
Java Assignment Operators
The Java assignment operators assign a value from a logic construct on the right-hand
side of the assignment operator to a variable on the left-hand side of the assignment op-
erator. The most common assignment operator is also the most commonly used operat-
or in the Java programming language, the
equals operator
. The equals operator can be
prefaced with any of the arithmetic operators to create an assignment operator that also
performs an arithmetic operation, as can be seen in
Table 3-5
. This allows a more
“dense” programming statement to be created when the variable itself is going to be
part of the equation. Thus, instead of having to write
C = C + A
, you can simply use
C+=A
and achieve the same end result. You will be using this assignment operator
shortcut often in your game logic design.