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
@@ -52,36 +52,45 @@ Let's implement keyboard controls to move our slime sprite around the screen. O
52
52
privateconstfloatMOVEMENT_SPEED=5.0f;
53
53
```
54
54
55
-
2. Next, in [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)), checkiftheup, down, left, orrightarrowkeysarepressed, andifanyofthemare, adjusttheslime's position. Add the following just before the call to `base.Update`:
3. Finally, in [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)), updatethepositionoftheslimewhenitisrenderedbyusingthe `_slimePosition` value:
87
+
3. Next, in [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)), callthenew `HandleKeyboardInput` methodjustbeforethecallto `base.Update`:
88
+
89
+
```cs
90
+
HandleKeyboardInput();
91
+
```
92
+
93
+
4. Finally, in [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)), updatethepositionoftheslimewhenitisrenderedbyusingthe `_slimePosition` value:
85
94
86
95
```cs
87
96
_slime.Draw(_spriteBatch, _slimePosition);
@@ -147,22 +156,31 @@ Let's implement mouse controls to move the bat sprite around the screen to the p
147
156
>
148
157
>Wecouldhavejustaseasilysetthebat's position inside the [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent) method after creating the slime, but I wanted to demonstrate the importance of the call order relationship between [**Initialize**](xref:Microsoft.Xna.Framework.Game.Initialize) and [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent)
149
158
150
-
3. Next, in [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)), checkiftheleftmousebuttonispressed, andifso, adjustthebat's position to the position of the mouse cursor. Add the following just before the call to `base.Update`:
159
+
3. Next, addthefollowingmethodwhichchecksiftheleftmousebuttonispressed, andifso, adjuststhebat's position to the position of the mouse cursor:
151
160
152
161
```cs
153
-
MouseStatemouseState=Mouse.GetState();
154
-
155
-
if(mouseState.LeftButton==ButtonState.Pressed)
162
+
privatevoidHandleMouseInput()
156
163
{
157
-
_batPosition=mouseState.Position.ToVector2();
164
+
MouseStatemouseState=Mouse.GetState();
165
+
166
+
if(mouseState.LeftButton==ButtonState.Pressed)
167
+
{
168
+
_batPosition=mouseState.Position.ToVector2();
169
+
}
158
170
}
159
171
```
160
172
161
-
4. Finally, in [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)), updatethepositionofthebatwhenitisrenderedbyusingthe `_batPosition` value:
173
+
4. Next, in [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)), callthenew `HandleMouseInput` methodafterthe `HandleKeyboardInput` call:
162
174
163
-
```cs
164
-
_bat.Draw(_spriteBatch, _batPosition);
165
-
```
175
+
```cs
176
+
HandleMouseInput();
177
+
```
178
+
179
+
5. Finally, in [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)), updatethepositionofthebatwhenitisrenderedbyusingthe `_batPosition` value:
Let's implement gamepad controls as an alternative method of moving the slime sprite around. Open the *Game1.cs* file and perform the following:
352
370
353
-
1.In [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)), use the value of the left thumbstick to adjust the sprite's position. Since the value of the thumbstick is a range between `-1.0f`and `1.0f`, we can multiply those values by the `MOVEMENT_SPEED`. This will make the slime move slower or faster depending on how far in the direction the thumbstick is pushed. Add the following just before the `base.Update` call:
371
+
1.Add the following method which takes the value of the left thumbstick and uses it to adjust the sprite's position:
Anotherthingwecandowithagamepadistellittovibrate. Todothis, the [**GamePad**](xref:Microsoft.Xna.Framework.Input.GamePad) classhasa [**SetVibration**](xref:Microsoft.Xna.Framework.Input.GamePad.SetVibration(Microsoft.Xna.Framework.PlayerIndex,System.Single,System.Single)) methodthatrequirestheplayerindex, andthespeedoftheleftandrightvibrationmotors. Thespeedcanbeanyvaluefrom `0.0f` (novibration) to `1.0f` (fullvibration).
367
397
368
-
Let's adjust the current code so that when the A button is pressed on the gamepad, it gives a slight speed boost to the slime as it moves. When moving with a speed boost, we can apply vibration to the gamepad as feedback to the player. Update the code you just added to the following:
> Notice above that we use a `while` loop with [**TouchPanel.IsGestureAvailable**](xref:Microsoft.Xna.Framework.Input.Touch.TouchPanel.IsGestureAvailable) as the condition for the loop. The reason we do this is because when a user performs a gesture, such as a horizontal drag across the screen, very quickly, what can often occurs is a series of multiple small drag gestures are registered and queued.
524
-
>
525
-
> Each time [**TouchPanel.ReadGesture**](xref:Microsoft.Xna.Framework.Input.Touch.TouchPanel.ReadGesture) is called, it will dequeue the next gesture. So to ensure that we handle the complete gesture, we loop the gesture queue until there are none left.
557
+
>
558
+
> Each time [**TouchPanel.ReadGesture**](xref:Microsoft.Xna.Framework.Input.Touch.TouchPanel.ReadGesture) is called, it will dequeue the next gesture. So to ensure that we handle the complete gesture, we loop the gesture queue until there are none left.
Let's implement touch controls to move the bat sprite around the screen to the point that the screen is touched, similar to what we did for mouse controls in the [Implementing Mouse Input](#implementing-mouse-input) section above. Open *Game1.cs* and perform the following:
533
566
534
-
1.In [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)), check for a touch location and move the bat sprite to that location if a touch occurs. Add the following just before the `base.Update` call:
567
+
1.Add the following method which checks for a touch location and moves the bat sprite to that location if a touch occurs:
2. Next, in [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)), callthenew `HandleTouchInput` methodafterthe `HandleGamePadInput` call:
583
+
584
+
```cs
585
+
HandleTouchInput()
586
+
```
545
587
546
588
Ifyouhaveyourdevelopmentenvironmentsetupfor mobile development, running the game now, you can touch the screen to move the bat to the point that was touched.
547
589
@@ -626,4 +668,4 @@ In the next chapter, we'll learn how to track previous input states to handle si
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/10_input_management/index.md
+50-42Lines changed: 50 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -889,55 +889,63 @@ Let's update the input code in our game now to instead use the `InputManager` cl
889
889
InputManager.Update(gameTime);
890
890
```
891
891
892
-
3. Nowlet's update our game controls to use the `InputManager`. First replace the exit condition code with the following:
892
+
3. Next, removethe `if` statementin [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)) thatchecksforthe gamepad back button or the keyboard escape key being pressed and then exits the game.
>Noticehowwestoreareferenceto `GamePadInfo` for player one. This makes our code more readable and efficient since we don't need to access the `GamePads` array multiple times.
905
-
906
-
4. Finally, replace the keyboard, mouse, and gamepad movement controls we implemented previously with the following:
894
+
4. Finally, update the game controls to use the `InputManager`. Replace the `HandleKeyboardInput`, `HandleMouseInput` and `HandleGamePadInput` methods with the following:
907
895
908
896
```cs
909
-
if(InputManager.Keyboard.IsKeyDown(Keys.Up))
910
-
{
911
-
_slimePos.Y-=MOVEMENT_SPEED;
912
-
}
913
-
if (InputManager.Keyboard.IsKeyDown(Keys.Down))
914
-
{
915
-
_slimePos.Y+=MOVEMENT_SPEED;
916
-
}
917
-
if (InputManager.Keyboard.IsKeyDown(Keys.Left))
897
+
private void KeyboardInputCheck()
918
898
{
919
-
_slimePos.X-=MOVEMENT_SPEED;
920
-
}
921
-
if (InputManager.Keyboard.IsKeyDown(Keys.Right))
922
-
{
923
-
_slimePos.X+=MOVEMENT_SPEED;
924
-
}
925
-
926
-
if (InputManager.Mouse.WasButtonJustPressed(MouseButton.Left))
0 commit comments