Question

Here it is my method to get keyboard state and change texture based on which key is pressed.

private void CheckKeyboardAndUpdateMovement()
{
    KeyboardState keyboardState = Keyboard.GetState();
    if (keyboardState.IsKeyUp(Keys.Left)) { ChangeTexture(1); }
    if (keyboardState.IsKeyUp(Keys.Right)) { ChangeTexture(2); }
    if (keyboardState.IsKeyDown(Keys.Left))
    {
        Movement -= Vector2.UnitX;
        ChangeTexture(3);
    }
    if (keyboardState.IsKeyDown(Keys.Right))
    {
        Movement += Vector2.UnitX;
        ChangeTexture(4);
    }
    if ((keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Up)) && IsOnFirmGround())
    {
        Movement = -Vector2.UnitY * JumpHeight;
    }
}

It works if direction are pressed, but doesn't make its own job when nothing is pressed (just because both the IsKeyUp are true). Only the cases' order prevents the static texture to be shown while moving the sprite... My question is, how can I make a clean solution of this problem? I already have an idea, but I don't like it at all...

Was it helpful?

Solution

If you want another solution, but is still similar to yours.

enum Direction { Left = 1, Right = 2}
Direction dir = Direction.Left; //or whatever

private void CheckKeyboardAndUpdateMovement()
{
    KeyboardState keyboardState = Keyboard.GetState();

    ChangeTexture((int)dir);
    if (keyboardState.IsKeyDown(Keys.Left))
    {
        Movement -= Vector2.UnitX;
        ChangeTexture(3);
        dir = Direction.Left;
    }
    if (keyboardState.IsKeyDown(Keys.Right))
    {
        Movement += Vector2.UnitX;
        ChangeTexture(4);
        dir = Direction.Right;
    }
    if ((keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Up)) && IsOnFirmGround())
    {
        Movement = -Vector2.UnitY * JumpHeight;
    }
}

You can do something similar with walking sprites.

OTHER TIPS

The worst solution I thought on, it's to store the sprite direction when the last key has been pressed and make a check on the next update, but it will become a mess more the keys and textures I'll have to control...

// Having a "bool leftDirectionOnLastMovement;" (worst name ever seen)
private void CheckKeyboardAndUpdateMovement()
{
    KeyboardState keyboardState = Keyboard.GetState();
    if (keyboardState.IsKeyUp(Keys.Left) && leftDirectionOnLastMovement)
        ChangeTexture(1);
    if (keyboardState.IsKeyUp(Keys.Right) && !leftDirectionOnLastMovement)
        ChangeTexture(2);
    if (keyboardState.IsKeyDown(Keys.Left))
    {
        Movement -= Vector2.UnitX;
        ChangeTexture(3);
        leftDirectionOnLastMovement = true;
    }
    }
    if (keyboardState.IsKeyDown(Keys.Right))
    {
        Movement += Vector2.UnitX;
        ChangeTexture(4);
        leftDirectionOnLastMovement = false;
    }
    }
    if ((keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Up)) && IsOnFirmGround())
    {
        Movement = -Vector2.UnitY * JumpHeight;
    }
}

I really hope someone will suggest me a better way to do this...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top