Game Development Reference
In-Depth Information
Let's say you want to have a decision in your game as to which InvinciBagel death
animation is called when the InvinciBagel is hit (shot, slimed, punched, and so on).
The death animation routine (method) would be called, based on the InvinciBagel's
state of activity when he or she is hit, such as flying ( F ), jumping ( J ), running ( R ), or
idle ( I ). Let's say these states are held in a data field called ibState , of the type char ,
which holds a single character. The switch-case code construct for using these game-
piece state indicators to call the correct method, once a hit has occurred, would be:
switch (ibState) { // Evaluate ibState char and
execute case code blocks accordingly
case 'F' :
deathLogicFlying(); // Java method controlling
death sequence if InvinciBagel flying
break ;
case 'J' :
deathLogicJumping(); // Java method controlling
death sequence if InvinciBagel jumping
break ;
case 'R' :
deathLogicRunning(); // Java method controlling
death sequence if InvinciBagel running
break ;
default :
deathLogicIdle(); // Java method controlling
death sequence if InvinciBagel is idle
This switch-case logic construct evaluates the ibState char variable inside the eval-
uation portion of the switch() statement (note that this is a Java method) and then
provides a case logic block for each of the game-piece states (flying, jumping, running)
and a default logic block for the idle state (which is a logical way to set this up).
Because a game piece cannot be idle, running, flying, and jumping at the same
time, you need to use the break keyword to make each of the branches of this decision
tree unique (exclusive).
The switch-case decision-making construct is generally considered more efficient,
and faster, than the if-else decision-making structure, which can use just the if keyword
for simple evaluations, like this:
if (expression = true ) {
programming statement one;
Search WWH ::




Custom Search