Game Development Reference
In-Depth Information
{
m_Player.LastFrameChange = 0;
m_Player.AnimFrame++;
if (m_Player.AnimFrame > 7)
m_Player.AnimFrame = 0;
}
This code is fairly simple. The first line adds
frameTime
to the player object's
LastFrameChange
variable. Remember that
frameTime
is the parameter of the
UpdateScene()
method, and it contains the amount of time that has elapsed since
the previous frame. Next, we have an
if
statement that checks if the player ob-
ject's
LastFrameChange
variable has a value greater than
0.1
. If this is the case,
it means that it has been 1/10th of a second or more since the last time we changed
the animation frame, so we will change it again. Inside the
if
statement, we reset
the
LastFrameChange
variable to
0
, so we will know when to change the animation
frame again. The next line increments the value of the player object's
AnimFrame
variable. And lastly, we have another
if
statement that checks if the new value of
the
AnimFrame
variable is too large. If it is, we reset it to a value of
0
and the anim-
ation starts all over again.
Running the game
We are almost ready to run the game, but don't forget that you need to add the
Dis-
pose(bool)
method. In this program, there are only four objects it needs to dispose
off.Theyare
m_RenderTarget
,
m_Factory
,
m_TileSheet
,and
m_DebugBrush
.
Theyshouldbedisposedofinthemanagedsectionofthe
Dispose(bool)
method.
You can see this in the downloadable code for this chapter.
With the cleanup code in place, we are ready to run the game. As you can see, you
control a rather goofy robot. Note that the player sprites are in the
Robot.png
file
and the tile sheet is saved in the
TileSheet.png
file. Both of these files are, of
course, included with the downloadable code for this chapter. The screenshot follow-
ing the explanation shows what the game window looks like with the debug overlay
off.