Game Development Reference
In-Depth Information
Tip
If you don't have a game controller, then you won't see any of the output from the
previous code. The program would still work, but there would not be any debug
output since there is no game controller to get it from.
This test code is a simple group of if statements. The first if statement checks
if button 0 is pressed. If so, it writes a line of debug output to show you that it has
detected the button press. The second if statement checks if button 1 is pressed,
and if so, writes a debug message saying so. And the last two if statements do the
same for buttons 2 and 3 .
So, why are we using numbers here? The reason is because each joystick button
has an index that we use to refer to it. So for example, on my gamepad, button 0 is
the A button.
We need to add two more lines of code to our Dispose(bool) method now. They
will go in the managed resources section of the method. Here they are:
if (m_Joystick1 != null)
m_Joystick1.Dispose();
This simply checks if the Joystick object is null. If not, then we dispose of it.
Run the program and press the buttons on your game controller. If you press the but-
tons 0 , 1 , 2 , or 3 , you will see some new lines of debug output appearing on Visual
Studio's Output pane. When one of these buttons we coded for is pressed, its mes-
sage appears multiple times. This is due to the speed at which the game loop is run-
ning.Itisrunningsuperfastrightnowsinceitdoesn't evenhavetorenderanygraph-
ics or simulate anything yet! The downloadable code for this chapter adds more if
statements to cover more buttons than we did here. It also has some commented
out lines for displaying the current positions of the left and right joysticks, and the
position of the axis that is being used to represent the triggers (these are the buttons
you can press in a little bit, all the way, or not at all, and they are usually found on
the shoulders of a gamepad style controller).
Search WWH ::




Custom Search