Game Development Reference
In-Depth Information
3.3.12 Different Kinds of Methods
We also see in the headers of both the
Update
and the
Draw
methods that there is a
difference with respect to the
Main
method. Look at the
Draw
method for example:
protected override void
Draw(GameTime gameTime)
There are some words:
protected
,
override
and
void
, then the name of the method
(
Draw
) and then some stuff between parentheses. Not all of this is relevant for now,
but the three words before the name of the method give some information about
what kind of method it is and how it can be used. In particular, the
override
word
means that this method is a
replacement
for a method, namely the method
Draw
in
the
Game
class. Recall that
BasicGame
was a special version of the
Game
class? The
Game
class already has a method called
Draw
. But since we want to do something
different in this method (such as setting a different background color), we want to
make our
own version of this method
.Theword
override
tells the compiler that we
are replacing the original method in the
Game
class by a method of our own, like
replacing an ingredient in a recipe to make it taste differently. Later on in this topic,
we will see many other examples of overriding methods and why this is useful. Let
us leave those two other words (
protected
and
void
) a mystery for the moment. We
will come back to it later.
Behind the name of the
Draw
method, we see parentheses with two words be-
tween them:
GameTime
and
gameTime
(note the case difference of the first charac-
ter). These two words together define a
parameter
of a certain
type
. The type in this
case is
GameTime
and in order to use this parameter inside the method, we have to
give it a name,
gameTime
. The type is telling the compiler what
kind of information
needs to be given as a parameter when this method is called. In this case, we want
a parameter that gives us the current time. But you could imagine that there are
other types of parameters. For example, the
Clear
method needs a
Color
as a param-
eter. The reason that the compiler needs to know the type of a parameter is because
that type determines how much memory is needed to store that particular informa-
tion. Also, it would be silly to pass a color to a method that needs a time, or vice
versa.
3.3.13 The Graphics Device
Before we can draw anything, we need to initialize the
graphics device
. This de-
vice controls the graphic capabilities of the game, so it is very important! In all
your games built using the XNA engine, you will need to initialize the graphics
device. Initializing the graphics device is something that we need to do before the
actual game starts, otherwise, we cannot draw anything. Also, we need to do this
initialization only once. Generally, we initialize the graphics device in the method