Game Development Reference
In-Depth Information
Getting input from the joystick
This is all well and good, but we still can't get input from a joystick. So let's look at that
now. First, we need to add three member variables for our joystick, just like we did for
the mouse and keyboard. Here are the three new member variables we need to add
to our UserInput.cs class:
Joystick m_Joystick1;
JoystickState m_Joy1StateCurrent;
JoystickState m_Joy1StateLast;
As before, we have a variable to hold our device object (in this case, a Joystick
object), and two more variables to hold the joystick state for the current frame and for
the previous frame.
Now, we need to add two lines at the bottom of our constructor to initialize the joystick
state variables. As discussed earlier in this chapter, this prevents a crash from poten-
tially happening. Add these two lines at the end of the constructor:
m_Joy1StateCurrent = new JoystickState();
m_Joy1StateLast = new JoystickState();
Now, let's modify our GetJoysticks() method. We will simply make it use the first
joystick in the returned list of controllers. Here is the new code for the GetJoy-
sticks() method:
public void GetJoysticks()
{
IList<DeviceInstance> deviceList =
m_DirectInput.GetDevices(DeviceClass.GameController,
DeviceEnumerationFlags.AttachedOnly);
for (int i = 0; i < deviceList.Count; i++)
{
if (i == 0)
{
Search WWH ::




Custom Search