Today we'll continue with our using the controller as an input device in our XNA games. Here's a reminder of the module learning objectives for Module eight. Last time we used a controller to ease the thumb stick on the controller to move our character around, and keep it clamped in the window, which had nothing to do with the thumb stick itself. We just left that code alone. So this time we're going to move on and use one of the buttons on the gamepad, or the controller, to change to a new character when the player presses that button. So let's go to the code. We're starting from the code we ended with last time, as you recall. We moved around with the thumb stick, and we checked to make sure that the game pad was connected before we did. Now, we're going to use the A button to actually change characters. And so I'm going to take this closed curly brace. Which goes with the if statement. Remember we said that we only want to use gamepad input if the gamepad is connected. So, I'm going to delete that from there and I'm going to move it all the way to the end of the method. So now, any game pad processing we do will only be done if the gamepad is connected. Let's comment this out, just to demonstrate yet again that we can't use a press, that we really do need to use a click, even with the A button on the game pad. So, uncomment this code. We of course, no longer have a mouse variable. So let's say we'll change the comment, A button press code bad, and so instead of checking whether the mouse left button is pressed, we can check the gamepad. Recall, that this is a gamepad state object that we get by calling the gamepad get state method. So we'll take a look at the gamepad, and we'll access the buttons of the gamepad. And then we can pick which button we want to access. So, there's a big button and there's, you know, ab backstart, left shoulder is a button, left stick means you push down on the left thumb stick. So, we actually have quite a few buttons. I'll scroll here to show, we actually have quite a few buttons on a 360 controller, but we're just going to use the A button. And so when we F5, I press A and we get the same behavior we had when we were trying to just do a left mouse press. So that clearly doesn't work for us. And in fact I'm just going to delete all this code. Perhaps with some control, I will delete all this code. And I will uncomment this code. And we'll have to just have to make a couple of changes to it. And we'll be good to go. Okay, so, uncomment. Not cut, uncomment, well. All that code on the A button click, not left mouse click. And it might feel to you that there's no reason to fix the comments, but we want the comments to reflect how the code actually works. So not the A button press. So we don't need this anymore, because we're not using the mouse anymore. And here, we'll check gamepad, buttons. A, just as before. So we're checking if A is released, we're checking if the previous state was pressed, so this is a click, on the A button. [NOISE] Change to a random character, and of course we need to change this as well. To keep track of the previous state of the A button rather than the mouse left, left mouse button. [LAUGH] So gamepad buttons, A, like so. If I run it, I'm clicking the A button and sometimes I get the same one. But regularly, I don't as well, and we talked about that last time, how that works. So really, if we're doing a straight conversion from the mouse left button to the gamepad A button, that's it. That's all we need to do to get the same behavior from the A button that we used to get from the left mouse button. I do want to show you, though, that there is another way that we can do this, that essentially uses the same idea, but in a slightly different way. And so you might see lots of people processing buttons with the game pad in this way instead. So I'll show you this way as well. So I'm going to comment out this. And I'm going to replace it with a Boolean variable, previous, A, I'm going to say aPreviousReleased, and I'll set it to true. So essentially it's going to do the same sort of thing, it's going to keep track of whether the A button. Was previously released. So, since I described it that way, let's change our variable name. So, aButtonPreviouslyReleased. Okay, we'll come down here, back to the update code. And now we won't be checking this. We'll be checking aButtonPreviouslyReleased. Now, notice there's no equal equal here. I could, in fact say equal equal true, for example, but there's no reason to do that, because this is a Boolean variable. And so comparing a true or false to true or false, just wastes cpu cycle. There's no reason to do it. As I think about this, as I copy this here, I realize that I don't really care if the A button was previous released. What I really care about was, was it previously pressed? And so in fact, I can do something called refactoring. We've talked about refactoring as restructuring the code. I can refactor to rename this variable. And if I say aButtonPreviouslyPressed and say OK. And apply it. It changes it here, and it changes it here. In fact it changes it everywhere throughout my code. I actually want to now start though with this being false. So A was not previously pressed, however in our update. We'll check. If the aButton is currently released and it was previously pressed then we'll do this stuff. And in fact we will change this little bit and instead of doing that comparison to the button state we'll actually ask the game pad is. Button down. And we will have to provide it with one argument, which button we mean. So we'll say Buttons.A. Buttons is just an enumeration of all the possible buttons on the 360 controller. And so, this is a different way we check and find out if A is down. But that's not what we really want. We want if A is up, so it's released. And it was previously pressed so that's a click on the A button. We still have to change this though, because we got rid of that variable. So I'll just comment this out, and say aPreviously, aButtonPreviouslyPressed is going to just be gamepad.IsButtonDown Buttons A. So think about how this works. Basically, this is a Boolean variable, so it has to get a Boolean value. The is button down method returns a Boolean value, true or false, based on whatever button we provided. So this question over here is, is the A button down? And if this method returns true then the variable aButtonPreviouslyPressed gets set to true. If this method returns false then this get set to false, and so if we F5 again, I am getting the same behavior as before, it's changing when I click the A button. The way that you do this which which technique you use, whether you're doing is button up, is button down or whether you're keeping track of the button state of the buttons, is in many ways, personal preference. I have personally found that I tend to use the is button up, is button down thing more regularly when I'm processing a controller. But, you can do it the other way, too. And if you do it the other way, it's so similar to the way we process mouse input, mouse button input, that, that may be more intuitive to you because you do it sort of consistently across both input devices. So, what we've seen. At this point, is we've seen how we can use the mouse, both location and button. We've seen how we can use the controller, both for the thumb stick and a button, as input devices for our XNA games. And really that concludes in this module all the forms of input. That we want to deal with. However, there is one form of output that we can use with the 360 controller. It's called forced feedback and we are going to look at that next time. So we're all done with input, but we're going to look at output next time to finish off the module