Game Development Reference
In-Depth Information
public void
HandleInput()
{
dosomething...
}
Instead of a type like
float
or
bool
, the keyword
void
is written in front of the method
name.
void
means that the method does
not have a return value
, meaning that we
cannot store the outcome of this method in a variable. For example, the following
line would result in a compiler error:
float
oops = HandleInput();
Also, because this method does not have a return value, we do not need to use the
return
keyword inside the body of the method (although it can sometimes be useful,
as we will see). You will notice that whenever a method with no return value is
called, it has no result that can be stored in a variable and whenever a method does
have a return value, this value is used in some way. We can use the value to store it
in a variable, like in this example:
currentMouseState = Mouse.GetState();
The method
GetState
in the
Mouse
class has a return value of type
MouseState
,sowe
can store it in a member variable of the same type. The following example shows
the calling of two methods neither of which has a return value:
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin();
A return value does not necessarily have to be stored in a variable. We can also
directly use it in an
if
-instruction, like we do in the
HandleInput
method:
if
(currentKeyboardState.IsKeyDown(Keys.R) &&
previousKeyboardState.IsKeyUp(Keys.R))
currentColor = colorRed;
Here, the
IsKeyDown
and
IsKeyUp
methods return a value of type
bool
. The difference
between things that have a value and things that do not have a value is something
we have seen before: it is the same difference we saw between
instructions
(which
do not have a value) and
expressions
which
do
have a value. So, this means that
IsKeyDown(Keys.K)
is an
expression
, whereas
HandleInput();
is an
instruction
. A sec-
ond difference between these two things is that expressions never end with a semi-
colon, and instructions always end with a semicolon, except if the instruction is a
block.