Game Development Reference
In-Depth Information
Notice the conditionals in the
CheckInput()
function also changed slightly from two
if
conditionals
to an
if - else if
sequence. You could have left them as two
if
conditionals, but
else if
is more
efficient. As an example, assume you have created cheat keys for each of the seven zones. When
you use seven
if
conditionals, each conditional is checked each time the function is called.
Say the user presses the 3 key:
Processor
Code
Evaluates conditional: false
if Alpha1 key pressed
Evaluates conditional: false
if Alpha2 key pressed
Evaluates conditional: true
if Alpha3 key pressed
Processes code block contained in conditional
Evaluates conditional: false
if Alpha4 key pressed
Evaluates conditional: false
if Alpha5 key pressed
Evaluates conditional: false
if Alpha6 key pressed
Evaluates conditional: false
if Alpha7 key pressed
Evaluates subsequent code . . .
Using
else if
in a sequence of conditionals means that each conditional will be evaluated until one
is found to be true, and the remaining
else if
conditionals can be skipped once the true condition
has been identified.
Again, assume the user presses the 3 key:
Processor
Code
Evaluates conditional: false
if Alpha1 key pressed
Evaluates conditional: false
else if Alpha2 key pressed
Evaluates conditional: true
else if Alpha3 key pressed
Processes code block contained in conditional
Not evaluated
else if Alpha4 key pressed
Not evaluated
else if Alpha5 key pressed