Game Development Reference
In-Depth Information
instruction is
not
true, and we use the
else
keyword for that:
if
(mouse.LeftButton == ButtonState.Pressed)
{
double
opposite = mouse.Y
−
barrelPosition.Y;
−
double
adjacent = mouse.X
barrelPosition.X;
angle = (
float
)Math.Atan2(opposite, adjacent);
}
else
angle = 0f;
This instruction does exactly the same thing as the two
if
-instructions before, but we
only have to write down the condition once. Execute the
Painter1b
program and see
what it does. You will note that the angle of the cannon barrel is zero as soon as you
release the left mouse button.
The syntax of the
if
-instruction with an alternative is represented by the following
syntax diagram:
The reason that the body of an
if
-instruction can consist of multiple instructions
between braces is that an instruction can also be a
block
of instructions, which is
defined in the following syntax diagram:
6.4.2 A Number of Different Alternatives
When there are multiple categories of values, you can find out with
if
-instructions
which case we are dealing with. The second test is placed behind the
else
of the
first
if
-instruction, so that the second test is only executed when the first test failed.
A possible third test would be placed behind the
else
of the second
if
-instruction.
The following fragment determines within which age segment a player falls, so
that we can draw different player sprites: