In the previous blog entry I talked about moving your sprite using the D-Key on the Gamepad or the keyboard.  What if you want to use the thumbsticks on the gamepad?  This is a little more tricky because the thumbstick uses the Vector2 object, and that can't be used easily if you are not used to object oriented programming.  But unless you want to just do simple games you need to be able to use the output from the thumbsticks.  In this entry, I am not giving you working code, but rather code that you can think about.  This is important in figuring out how to write programs, sometimes you have to think about what you are trying to do.

In this case to utilize the thumbstick requires a way to read the 2-dimensional thumbstick (or sensor as it might be called). In engineering and software development, this would be a 2-D vector, you have an X and Y. The thumbstick is a member of the GamePad object and returns a complex memory object called a structure.  This blog entry is more a "thought" type of article, although make sure to play with the code to figure out where to place the code, etc.  Tomorrow I will have a video and upload the example code.

At the top of the code you would place the variable declarations:

public class Game1 : Microsoft.Xna.Framework.Game //this is the header of the Game1 class

{

GraphicsDeviceManager graphics;

SpriteBatch spriteBatch;public

float readStickRotationX, readStickRotationY; //this is the code you would add

//Other code follows here

In the Update Method add the following code, Vector2 constructs a memory object that will allow us to consume the output from the Thumbstick:

protected override void Update(GameTime gameTime)
{

        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

        Vector2 rotationAmount = -gamePadState.ThumbSticks.Left;

        // Scale rotation amount to radians per second
        rotationAmount = rotationAmount * RotationRate * elapsed;

        //Read in the position of the thumbstick, consideration for “deadzone” will be covered later
        readStickRotationX = rotationAmount.X;
        readStickRotationY = rotationAmount.Y;

//Other code here

To read the Thumbstick output into a text area on the screen, you need to set-up your fonts, which will be covered later. But here is how you would convert the output from a float to a string.

1. Make sure that your code has a line “using system;” at the top of the class you are using, some game examples do not include the “using system;” since pure XNA games don’t need to use system, but I always add it.

2. Place the following code into your Draw method or a method called from the Draw method (more on this later in the series):

string rotationX = Convert.ToString(ship.readStickRotationX);

string rotationY = Convert.ToString(ship.readStickRotationY);

3.  You then use the string in your text overlay, and that might look like the following:

spriteBatch.DrawString(spriteFont,rotationX, new Vector2(600, 300), Color.White);
spriteBatch.DrawString(spriteFont, rotationY, new Vector2(575, 275), Color.White);

If you find that the the code I have added is difficult to read, you might want to consider visiting this web site: