Game Development Reference
In-Depth Information
m_Joystick1 = new Joystick(m_DirectInput,
deviceList[0].InstanceGuid);
// Set the range to use for all of the
axis on our game controller.
m_Joystick1.Properties.SetRange(-1000,
1000);
}
}
}
As you can see, we've also added a second line inside the if statement. This sets
the minimum and maximum possible values for each axis on our game controller. In
this case, we set it to -1,000 and 1,000 . This means when the joystick is all the
way to the left, its horizontal position is -1,000 . When it is all the way to the right, its
horizontal position is 1,000 . The same is true for the vertical axis. When the joystick
is centered, its position will be (0,0). It is important to know the range of possible
values to make our controls work correctly. You can get the range from the Joy-
stick.Properties.LowerRange and Joystick.Properties.UpperRange
properties. Note that these properties can throw an exception in some cases de-
pending on your game controller's drivers and your DirectX version.
Now, we need to add a couple of lines of code into our Update() method to get
the latest joystick data. To do this, we first need to add a line at the beginning of this
method to acquire the joystick. You can't use a device without acquiring it first (see
the Mouse and keyboard input section of this chapter for information on acquisition
and why we need to do it). So, we will add the following line of code to acquire the
joystick for us:
m_Joystick1.Acquire();
We are basically letting the system know that we wish to use the joystick now and
get access to it. Now that we have gotten the access to the joystick, we need to add
these two lines at the end of the Update() method:
Search WWH ::




Custom Search