Friday, October 8, 2010

Input System: Part 2 Input Basics

So for the first part of the input class were creating the basic aspects of the input system. The input system will handle controls for the game pads and keyboard we will not be needing the mouse since the mouse does not work on the xbox and well I never really use it. Keyboard controls are good for simple debug commands such as pressing F for full screen or pushing a button to go back to the main menu without having to use a game pad button. Now were going to start with two functions, one that will initialize our variables since static classes can not have a constructor and another one to update our game pad and keyboard states so they will be named Initialize and Update. The class itself will just be called Input and will be a static class therefore our functions must be static as well. Here is what our class looks like so far except that your namespace will probably be different. 


Now were going to need to add in our variables for our game pad states and keyboard state. To check whether or not a button was just pressed we need to also know the previous states to compare with so we need two sets of both game pad states and keyboard state. The game pad states are an array since we need one for each controller with up to four controllers. These are going to be private variables since there only going to be used in this class. And because there in a static class they too need to be static.


 After we make our variable declarations lets then initialize the variables that need to be, which in this case is just the game pad states so its very simple here.


Next were going to create the update function. We need to call this function every frame so that the player will have the most up to date input commands. To update our two sets of states we just set the previous state to the current state then update the current state using either GamePad.GetState or Keyboard.GetState very simple.


Now after we have done that all we need to do in our Game1 class is to initialize our input in the Initialize method and Update our input in the Update method like so.


Although our input class isn't very helpful as of now it has the basics and from here we can add numerous amounts of functions to get the controls we want which will begin in part 3 which hopefully will not take too long to make.


No comments:

Post a Comment