Difference between revisions of "Mouse or touch control Input"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "This page is to document how to you can handle mouse and touch input. You could essentially program all inputs within your player movement code. The page about the keyboard in...")
 
Line 2: Line 2:
  
 
==Adding GamePad Control==
 
==Adding GamePad Control==
With in the Game1.cs file, find the the Game1 class and find the line Player player; . After this line enter the following, this will be used to save the current and previous keyboard state and to also control the speed of movement:
+
With in the Game1.cs file, find the the Game1 class and find the line Player player; . After this line enter the following, this will be used to save the current and previous keyboard state and to also control the speed of movement (you may already have this if you have already coded the keyboard input):
  
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
// Keyboard states used to determine key presses
+
// Gamepad states used to determine button presses
KeyboardState currentKeyboardState;
+
GamePadState currentGamePadState;
KeyboardState previousKeyboardState;
+
GamePadState previousGamePadState;
  
 
// A movement speed for the player
 
// A movement speed for the player
Line 20: Line 20:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The Update method within the Game1.cs file is used while the game is running. In order to make this easy to understand and read, we will create a new method to update the player and call it from the Update method. Create the UpdatePlayer method and call it from Update, see below:
+
The Update method within the Game1.cs file is used while the game is running. In order to make this easy to understand and read, we will create a new method to update the player and call it from the Update method. If you haven't already created the UpdatePlayer method, create the UpdatePlayer method and call it from Update, see below:
  
 
[[File:Update player.png]]
 
[[File:Update player.png]]

Revision as of 14:50, 28 March 2017

This page is to document how to you can handle mouse and touch input. You could essentially program all inputs within your player movement code. The page about the keyboard input is: Adding and Processing Player Input.

Adding GamePad Control

With in the Game1.cs file, find the the Game1 class and find the line Player player; . After this line enter the following, this will be used to save the current and previous keyboard state and to also control the speed of movement (you may already have this if you have already coded the keyboard input):

// Gamepad states used to determine button presses
GamePadState currentGamePadState;
GamePadState previousGamePadState;

// A movement speed for the player
float playerMoveSpeed;

Now find the Initialize method within the Game1.cs file. You need to add the line below to set the movement speed:

// Set a constant player move speed
playerMoveSpeed = 8.0f;

The Update method within the Game1.cs file is used while the game is running. In order to make this easy to understand and read, we will create a new method to update the player and call it from the Update method. If you haven't already created the UpdatePlayer method, create the UpdatePlayer method and call it from Update, see below:

Update player.png