[SOLVED] isButtonDown behaviour

I’m working on a little game right now, but ran into something strange. First, I read my states into variables, like so:

GamePadState gs1 = GamePad.GetState(PlayerIndex.One); [...] KeyboardState ks = Keyboard.GetState();

Then I keep everything into booleans:

//keyboard controls bool leftkey = ks.IsKeyDown(Keys.Left); bool rightkey = ks.IsKeyDown(Keys.Right); [...] //joystick controls bool p1_leftpad = gs1.IsButtonDown(Buttons.DPadLeft); bool p1_rightpad = gs1.IsButtonDown(Buttons.DPadRight);

However, when I call it on Update, the behaviour differs between keyboard and gamepad.

I’m doing it like this:

//slide to the left if (leftkey || p1_leftpad) { if (myCharacter.Sprite.CurrentAnimation != "moveLeft" && !upkey && !downkey && !p1_uppad && !p1_downpad) { myCharacter.Sprite.CurrentAnimation = "moveLeft"; } myCharacter.Sprite.MoveBy(-2, 0); }

Back when I was just using the keyboard, everything was working just fine, but now that I’m using the gamepad too, the animation keeps looping forever once I start playing it. I’m using everything pretty much the same as with the keyboard. What am I missing? Do I really have to use that whole “last state” thing with the gamepad?

Edit: I’ve also tried using just the gamepad, but the results are the same.

Replace the || with |

|| means only check the next expression if the first one is true

Also the whole checking the keys and gamepads is complex simplify it by doing

bool left = ks.IsKeyDown(Keys.Left) | gs1.IsButtonDown(Buttons.DPadRight);
Do the same for all directions. Then u just have 4 variables to check

This is wrong. || is the or operator and it only checks the second operand if the first one is false, since there’s no point in evaluating the second one if the first is true. You pretty much always want to use || and && except when you write bad code and your second operand has a side effect (like it modifies a variable and you always want that executed, but you really shouldn’t be writing code like this).

Are gamepad buttons not detected at all, or are the down/up dpad buttons down according to MG when they aren’t?

They are detected and work fine. I can move my character around, both with the keyboard and with the d-pad on the controllers.

However, since I started using the gamepad, the animation keeps looping forever.

i.e.: if I move left, the “move left” animation plays forever, even though the caracter isn’t moving anymore.

It worked fine when I was using just the keyboard.

lol really must have had a moment of been dumb there. thanks for the catch.

Then the issue is probably in your logic. Do you expect an idle animation or something? Can you show the code responsible for changing the animation? It might help to abstract the keyboard and gamepad input so you don’t need to check then separately in your logic.

To do the animation, I just use a bunch of classes I got from a XNA Resources tutorial; you can find it all here: http://www.xnaresources.com/default.asp?page=Tutorial:SpriteEngine:1
There’s a file by page 5 with everything done, in case you want to look into that. I didn’t change anything in these classes, except for a few comments.

What puzzles me is that it works fine with the keyboard, but glitches with the gamepad with nothing else being different.

I meant if you can show the code where the animation should be changed from moveLeft to something else. I.e. where it should stop looping.

Oh, yes. It goes back to the “idle” animation right here:

if (!leftkey && !rightkey && !upkey && !downkey && !p1_downpad && p1_leftpad && p1_rightpad && p1_uppad) {if (myCharacter.Sprite.CurrentAnimation == "moveLeft") { myCharacter.Sprite.CurrentAnimation = "idle"; }[...]

(it’s ugly I know, but again, works for the keyboard)

Looks like you forgot to negate these.

Whoa, can’t believe I forgot about those. Guess that’s what happens when you keep copying and pasting code around.

HOWEVER, I negated those, cleaned and rebuilt the project, and the issue still persists :frowning:

I thought it’d fix the behaviour, but it still looks like I’m holding the key down. Any other ideas?

Just to update this: it suddenly started to work. I was working on other stuff and the project was renamed once because of another suspected bug, so maybe that was it, but the animations and gamepads are now working flawlessly.

1 Like