Troubleshooting this TutorialSometimes, even though you try hard to understand the information in a tutorial, things don't work out quite like you want it to. This page is here to help you resolve any problems you might be having with simple Xbox 360 controller input. The Common Mistakes section describes common problems that people have when doing the things in this tutorial, and how to resolve them. The Frequently Asked Questions section describes questions that people have that aren't related to mistakes, but rather, trying to understand the stuff better or exploring how it can be used. If your problem or concern isn't addressed here, feel free to add a comment below, so that I know where you're having trouble. I like to keep these pages fairly clean, so I may remove comments that I felt like have been addressed. If I remove your comment and you don't feel like the problem has been fixed, repost the question and we'll take another look at it. If a tutorial has a mistake in it, I will fix the mistake and reply to the comment with a brief explanation. However, after a couple of weeks I'll likely go back and remove the original comment as well as my reply, because, hopefully, the problem will have been fixed, and it won't be a concern any more. |
Common MistakesNone listed yet… |
Frequently Asked QuestionsNone listed yet… |


Hi,
i have a short question.
If I do the following in the Update-Method, I get the same result as if'd use the commented out code with the two variables currentKeyState and prevKeyState.
currentKeyState = Keyboard.GetState();
//if (currentKeyState.IsKeyDown(Keys.Down) && prevKeyState.IsKeyUp(Keys.Down))
// displayText = true;
//else if (currentKeyState.IsKeyUp(Keys.Down) && prevKeyState.IsKeyDown(Keys.Down))
// displayText = false;
if (currentKeyState.IsKeyDown(Keys.Down))
displayText = true;
else if (currentKeyState.IsKeyUp(Keys.Down))
displayText = false;
//prevKeyState = currentKeyState;
So my question is:
Is this generally a way to do this, or is this just something that works, because I only check one button?
I feel that I got something wrong, but cant exactly determine what is wrong :)
Many thanks in advance,
André
Ok, i think i got it (maybe this attempt to clarify my issue on my own helps somebody if he is here for the same reason):
If I'd use the not commented out code above, i would have the optically same result (text disappears when lifting the down button), but if I'd repeat the code under the if-Statement over and over again, as long as the button is pressed.
So this would not help if I want to happen something only one time per button press.
This is my conclusion so far. Please correct me if I am wrong :)
Best regards,
André
What you're describing sounds right. In the commented out code, it happens only right when the button is first pressed or released. The other code, while simpler, will happen every single update that the key is up (or down). Since you're just setting a Boolean variable, either way works here. But if, for instance, you were using this key to fire a ship's weapons, with the commented out code, it only happens when you first press the key. So you'd have to repeatedly tap it to fire the guns. With the uncommented code, every update (60 or 30 times a second? Can't remember off the top of my head right now.) you'd fire another shot. (Probably, if that's what you're trying to do, you'll want to build in some sort of timing mechanism to fire 10 times a second as long as the key is down, rather than every update.)
But yeah, I think your description in your second post is exactly correct.
I can't figure this out.
Both functions work independently so I'm only going to type one.
public bool aButtonPress()
{
if (previous.Buttons.A == ButtonState.Released && current.Buttons.A == ButtonState.Pressed)
{
previous = current;
return true;
}
previous = current;
return false;
}
If I put aButtonPress() over bButtonPress() then only aButtonPress() will execute and vice versa. Why is that?
I'd have to see the code that calls this to be sure, but I think the problem is that you're assigning previous = current;. Those are class level instance variables, right? (As in, defined up at the top of the class, outside of any method, but inside of the class definition itself.)
Basically, in this method, you're checking whether the old state and the new state are different. But because you're assigning current to previous, in the second method (bButtonPressed?) the two will be the same object. The button states will never be different, and you'll never see the button presses, as you're describing.
I'd structure it a little more like this (though note I'm not using a compiler, and things may not be exact or even compile):
Does that make sense? Does it accomplish what you were trying to do?
Yes, that worked! I have been trying to figure this out for weeks.
This is what I ended up with.
public void update()
{
GamePadState current = GamePad.GetState(PlayerIndex.One);
if (current.Buttons.A == ButtonState.Pressed && previous.Buttons.A == ButtonState.Released)
{
for (int i = 0; i < 12; i++)
buttons[i] = false;
buttons[0] = true;
}
if (current.Buttons.B == ButtonState.Pressed && previous.Buttons.B == ButtonState.Released)
{
for (int i = 0; i < 12; i++)
buttons[1] = true;
}
previous = current;
}
public bool aPress()
{
return buttons[0];
}
Thank you so much. I couldn't take much more frying my brain over that one. I am more than likely going to buy your book as well.
Take care.
Ben
Awesome! Glad you got it to work!
Hi =)
To create a xbox360 game do i need to change something in my program code except the input lines or it is exactly the same like a windows game?
Generally, the input is the only change that is required. In XNA Game Studio, you just create a copy of your project for the Xbox 360, and away you go. Note, there's a specific menu option for this that allows two different projects that build for different target environments, while maintaining the exact same code base for the two. Don't actually go copy the project directory on your hard drive, or you'll have to maintain two code bases for Windows and the Xbox. You don't want that.
In practice, though, there may be a little more work to do than just code a different input device. For instance, you want to think through how the devices are different, how the user bases are different, and how the thing is actually used. Not every game that's good for the PC is good for a console, and not every game that's good for a console is good for the PC.
As an example, RTS games are usually not a good fit for consoles. You just don't have the right input (mouse and keyboard) to select units and give them specialized orders very well. (The only console RTS game of consequence that I've seen is Halo Wars. They clearly did everything they could to design it for a console, and I'm not sure if it would be very useful, in exactly the same format, on a PC. It requires a different strategy.
So while the code itself will work on both platforms, there's more to consider than that. I suspect whatever game you want to make, it is worth spending time thinking about what platforms you want to target, and consider how things will be different among them.
Hmm… one other area that is worth pointing out, which you'll likely find will require different code is any networking for multi-player. On the Xbox, they've got things strapped down pretty tightly. You have to code things the right way (I apologize that I don't really have networking tutorials here yet) and your users will likely need to pay for a subscription to use the network on the Xbox. The rules are very different on the PC. Of course, this all depends on you wanting networked multi-player games to begin with.
I've probably gone on long enough for now, but just to summarize, with only a few exceptions your code will work in both places without requiring changes, but you should still consider how the different platforms are commonly used, and their limitations when designing your game.
Where can I download this library at?
Post preview:
Close preview