Skip to content

Commit

Permalink
Some minor formatting updates and API links
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDarksideJ authored Apr 25, 2024
1 parent 52dc18e commit 03c3fd8
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions articles/getting_started/5_adding_basic_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,16 @@ The rest of the lines of code do the same thing but for the Down, Left and Right

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

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:
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).

Find the **Update** method in the Game1.cs class file and add:

```csharp
if(Joystick.LastConnectedIndex == 0)
{
JoystickState jstate = Joystick.GetState(0);
JoystickState jstate = Joystick.GetState(PlayerIndex.One);

float updatedBallSpeed = ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
float updatedBallSpeed = ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

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

The following is a line-by-line analysis of the above code.

```csharp
if(Joystick.LastConnectedIndex == 0)
```

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.
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.
If there is no controller, the code inside the if statement will be skipped over.

```csharp
JoystickState jstate = Joystick.GetState(0);
```

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

```csharp
if (jstate.Axes[1] < 0)
```

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.
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]').
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]`.

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

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.
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.

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.

Expand Down

0 comments on commit 03c3fd8

Please sign in to comment.