Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm doing a game with XNA and I need an animation when two keys are pressed.. I tried this but it doesn't function.

if (Keyboard.GetState().IsKeyDown(Keys.D) && Keyboard.GetState().IsKeyDown(Keys.W))
{
    jumpRight(gameTime);
    vel.X = +2;
}

The jump functions and the others animations with only one key pressed function too, but this one doesn't work. What should I do?

share|improve this question

closed as too localized by msell, Anko, Byte56, John McDonald, bummzack May 1 '13 at 20:53

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
The code seems alright, care to update your post with a little bit more code? I'm particularly interested in your single-key animations as you say those work as expected. –  Lennard Fonteijn May 1 '13 at 12:00
    
if (Keyboard.GetState().IsKeyDown(Keys.D)) { animazioneDestra(gameTime); velocita.X = 3.5f; } –  user30226 May 1 '13 at 12:02
    
If, for testing purporses, you change that line to: if (Keyboard.GetState().IsKeyDown(Keys.D) && Keyboard.GetState().IsKeyDown(Keys.W)) { animazioneDestra(gameTime); velocita.X = 3.5f; }, does it still work when you press both W and D? –  Lennard Fonteijn May 1 '13 at 12:04
    
yes it does.... –  user30226 May 1 '13 at 12:07
    
Post your entire key handling code in your question. It could potentially be the way you setup your logic. –  Jon May 1 '13 at 12:10

3 Answers 3

I would recommend using Console.WriteLine("some text") in the future to determine whether or not your code gets reached, in this case:

if (Keyboard.GetState().IsKeyDown(Keys.D) && Keyboard.GetState().IsKeyDown(Keys.W)) 
{
    Console.WriteLine("D and W pressed!");

    jumpRight(gameTime); 
    vel.X = +2;
}

If the message is printed, it could be an error in your jumpRight-method.

If the message is NOT printed, it could be an error in the way you setup your logic.

Alternatively, you could learn how to debug your code, for that, refer to this neat little tutorial: http://www.dotnetperls.com/debugging

EDIT:

To answer your question in this particular case, instead of else if in say else if (Keyboard.GetState().IsKeyDown(Keys.A)) use just if.

Why? Because when you press A, it does what it has to do when you press A, and then skips over the rest of the if-statement blocks. If you use if instead, it won't.

Your new Update-method would look like this (I also removed my earlier suggested code):

public void Update(GameTime gameTime)
{
    NS = VS;
    rect = new Rectangle(frameCor * largFrame, 0, largFrame, altFrame);
    origine = new Vector2(rect.Width / 2, rect.Height / 2);
    posizione = posizione + velocita;

    velocita = Vector2.Zero;

    if (Keyboard.GetState().IsKeyDown(Keys.D))
    {
        animazioneDestra(gameTime);
        velocita.X = 3.5f;
    }

    if (Keyboard.GetState().IsKeyDown(Keys.A))
    {
        animazioneSinistra(gameTime);
        velocita.X = -3.5f;
    }

    if (NS.IsKeyDown(Keys.D) && VS.IsKeyDown(Keys.W))
    {
        SaltoDX(gameTime);
        velocita.X = +3;
    }

    if (Keyboard.GetState().GetPressedKeys().Length == 0)
    {
        Stato(gameTime);
        velocita.X = 0;
    }
}
share|improve this answer

Please note that although you said that you wanted to check if D and W keys were pressed simultanously, you don't. What you do is, you check if the D key is down. Next, you check if the W key is down. Nothing in your code makes sure that both are down at the very same time. Any time you call GetState you query the keyboard for it's state. In your code, you check two different keyboard states. If you want to check if both keys are down in the same instant of time you need to check it on the same keyboard state.

// get the state once
var state = Keyboard.GetState();

// check if D and W are down in the same state
if (state.IsKeyDown(Keys.D) && state.IsKeyDown(Keys.W))
{
    // code
} 
share|improve this answer

Your NS and VS KeyboardState objects are never used. Because of this, you are never checking against your current KeyboardState.

You will need to do something like:

VS = Keyboard.GetState()
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.