Skip to content

Commit 03c3fd8

Browse files
Some minor formatting updates and API links
1 parent 52dc18e commit 03c3fd8

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

articles/getting_started/5_adding_basic_code.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,16 @@ The rest of the lines of code do the same thing but for the Down, Left and Right
125125

126126
If you run the game, you should be able to move the ball with the arrow keys.
127127

128-
Another option for user input is the Joystick class. Setting up input for the Joystick is very similar to setting up keyboard input. Find the **Update** method in the Game1.cs class file and add:
128+
Another option for user input is the <xref:Microsoft.Xna.Framework.Input.Joystick> or <xref:Microsoft.Xna.Framework.Input.GamePad> classes. Setting up input for Joysticks and GamePads is very similar to setting up keyboard input, the following example is designed while using a single joystick connected to the host, to support more, you will need to evaluate all the connected joysticks and read their input (see <xref:Microsoft.Xna.Framework.Input.JoystickState#Microsoft_Xna_Framework_Input_JoystickState_IsConnected> for reference).
129+
130+
Find the **Update** method in the Game1.cs class file and add:
131+
129132
```csharp
130133
if(Joystick.LastConnectedIndex == 0)
131134
{
132-
JoystickState jstate = Joystick.GetState(0);
135+
JoystickState jstate = Joystick.GetState(PlayerIndex.One);
133136

134-
float updatedBallSpeed = ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
137+
float updatedBallSpeed = ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
135138

136139
if (jstate.Axes[1] < 0)
137140
{
@@ -154,29 +157,30 @@ if(Joystick.LastConnectedIndex == 0)
154157
```
155158

156159
The following is a line-by-line analysis of the above code.
160+
157161
```csharp
158162
if(Joystick.LastConnectedIndex == 0)
159163
```
160164

161-
This code assumes that we have a single controller plugged into our device. LastConnectedIndex is the index of the last connected controller. The default is -1, which would mean no controller is plugged in.
165+
This code assumes that we have a single controller plugged into our device. `LastConnectedIndex` is the index of the last connected controller. The default is `-1`, which would mean no controller is plugged in.
162166
If there is no controller, the code inside the if statement will be skipped over.
163167

164168
```csharp
165169
JoystickState jstate = Joystick.GetState(0);
166170
```
167171

168-
This code fetches the current joystick state ('Joystick.GetState(0)') and stores it into a variable called **jstate**.
172+
This code fetches the current first joystick state `Joystick.GetState(0)` and stores it into a variable called **`jstate`**.
169173

170174
```csharp
171175
if (jstate.Axes[1] < 0)
172176
```
173177

174178
This line checks whether the second Joystick axis is less than 0. The Joystick class stores multiple axis of direction for anything with an integer based range. For any number of 2D axis sticks, it stores it in an x,y format inside of an integer array.
175-
The axis of movement for 2D joysticks goes from -32768 to 32768 on most modern controllers. Aiming the Joystick upwards results in a negative value on the Y-axis ('Axes[1]').
179+
The axis of movement for 2D joysticks goes from -32768 to 32768 on most modern controllers. Aiming the Joystick upwards results in a negative value on the Y-axis `Axes[1]`.
176180

177181
The rest of the lines of the code do the same thing but for their relevant x and y directions.
178182

179-
If you run the game, you should be able to move the ball with the left Joystick on your controller if one is plugged in.
183+
If you run the game, you should be able to move the ball with the left Joystick on your controller if one is plugged in. For GamePads, just use the `GamePad` versions of the same `JoyStick` classes, but remember, GamePads usually have multiple "sticks" for the left and right hand sides of the controller.
180184

181185
You will probably notice that the ball slightly moves on its own. This will likely be the result of your Joystick having a slight drift. You can fix that by adding a deadzone and changing the conditions to use this deadzone.
182186

0 commit comments

Comments
 (0)