Game Development Reference
In-Depth Information
of several integer values, such as the number of seconds passed, the number of mil-
liseconds passed, and so on. For example, this expression gives us the milliseconds
fraction of the total time passed since the game has started:
gameTime.TotalGameTime.Milliseconds
For example, if 13 minutes, 12 seconds, and 345 milliseconds have passed since
the game has started, the result of this expression will be 345. In this example,
TotalGameTime
is a property of the type/class
GameTime
and this gives us a variable
of type
TimeSpan
, which in turn has a property called
MilliSeconds
. By the way, there
is also a way to obtain the total passed game time in milliseconds, which would be
792,345 milliseconds (
(
13
∗
60
+
12
)
∗
1000
+
345). You can get this value with the
following expression:
gameTime.TotalGameTime.TotalMilliseconds
Since the parameter
gameTime
is of type
GameTime
, we can make use of all these
properties to obtain information contained within the variable. Once we have this
information, we can store it in another variable (or do something else with it):
int
red = gameTime.TotalGameTime.Milliseconds;
We are now going to use the variable
red
to create yet another variable, but one of
the type
Color
. We will store a new color in this variable. Once we have done this,
we can pass it as a parameter to the method
Clear
of the
GraphicsDevice
class. Let us
first declare this variable:
Color background;
We name this variable
background
and the type of
background
is
Color
. Now that we
have declared this variable, we can give it some value. Because the
Color
type is quite
a complicated type, we cannot simply assign it a number like we did with the integer
type. A
Color
variable needs to be
constructed
from three different numbers: the
R (red), G (green), and B (blue) values. Constructing a value of a more complicated
type is done with the keyword
new
and the name of the type written after it. We
can then pass the information needed to construct the value as parameters between
parentheses. For example, we can create the color
black
and store it in a variable as
follows:
Color blackColor =
new
Color(0, 0, 0);
The three parameters given to construct this variable are separated by a comma
and they are written between parentheses. After creating the
blackColor
variable, the
memory will look like this: