Game Development Reference
In-Depth Information
m_Player.PositionX -=
PLAYER_MOVE_SPEED;
}
}
This code starts with a compound
if
statement, checking whether the user is press-
ing the
A
key or the left arrow key. Yes, you can control our game character using
either of the
W
,
A
,
S
, or
D
keys or the arrow keys if you wish to move the character
using the keyboard. Next, we have another
if
statement. This
if
statement checks
to see if moving the player to the left will cause a collision. If not, we move the player
to the left. As you can see, we use the
PLAYER_MOVE_SPEED
constant that we cre-
ated earlier in this chapter to control how much the robot moves. Obviously, we need
three more of these
if
statements to handle the right, up, and down directions. As
the code is very similar, I will not describe it here.
Note
The downloadable code for this chapter also supports controlling the robot using
joysticks/gamepads. It adds a member variable named
m_UseDirectInput
to
the
TileGameWindow
class. Set this variable to
true
to use DirectInput for joy-
stick/gamepad controls, or set this variable to
false
to have the program use
XInput for joystick/gamepad controls. We need the
m_UseDirectInput
mem-
ber variable because if we used both DirectInput and XInput at the same time for
the same game controller device, this will cause the player to get moved twice
per frame.
Animating the player character
With the user input and collision detection code done, there is now only one thing
left to do in
UpdateScene()
. We need to add a bit of code to animate the player
character:
m_Player.LastFrameChange += frameTime;
if (m_Player.LastFrameChange > 0.1)