You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Theaxisofmovementfor 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]`.
180
+
181
+
The rest of the lines of the code do the same thing but for their relevant x and y directions.
182
+
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.
184
+
185
+
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.
186
+
187
+
```csharp
188
+
public class Game1 : Game
189
+
{
190
+
Texture2D ballTexture;
191
+
Vector2ballPosition;
192
+
floatballSpeed;
193
+
194
+
intdeadZone;
195
+
```
196
+
197
+
Next, you need to initialize the deadzone. Find the **Initialize** method and add the following lines.
198
+
199
+
```csharp
200
+
deadZone=4096;
201
+
```
202
+
203
+
Now, replace the conditions for the Joystick movement in **Update** to the following:
204
+
205
+
```csharp
206
+
if (jstate.Axes[1] <-deadZone)
207
+
{
208
+
ballPosition.Y-=updatedBallSpeed;
209
+
}
210
+
elseif (jstate.Axes[1] >deadZone)
211
+
{
212
+
ballPosition.Y+=updatedBallSpeed;
213
+
}
214
+
215
+
if (jstate.Axes[0] <-deadZone)
216
+
{
217
+
ballPosition.X-=updatedBallSpeed;
218
+
}
219
+
elseif (jstate.Axes[0] >deadZone)
220
+
{
221
+
ballPosition.X+=updatedBallSpeed;
222
+
}
223
+
```
224
+
225
+
If you run the game and move the Joystick around, you should notice that your Joystick has to move a decent distance before the ball starts moving. This is what a deadZone does, it allows for there to be a minimum distance before the input is reflected in the game.
226
+
> Try experimenting with what happens when you change the value of the deadZone. Mess around and find an amount that fits your project.
227
+
128
228
You will probably notice that the ball is not confined to the window. You can fix that by setting bounds onto the ballPosition after it has already been moved to ensure it cannot go further than the width or height of the screen.
0 commit comments