forum

Home / DeveloperSection / Forums / Change texture on key down

Change texture on key down

Anonymous User 2137 12-Dec-2013
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;
    }
}

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

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?


c# c# 
Updated on 12-Dec-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By