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.
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).
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.
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.