Search code examples
c#xna-4.0

How do i check if there are no key inputs on xna


public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D walkRight, walkUp, walkLeft, walkDown, currentWalk, TitleScreen, stand;
    Rectangle destRect;
    Rectangle sourceRect;
    KeyboardState ks;
    Vector2 position = new Vector2();
    bool isGrounded;
    bool isStanding;
    float fallSpeed = 5;

    enum GameStates { Titlescreen, Playing, PlayerDead, GameOver };
    GameStates gameStates = GameStates.Titlescreen;

    float elapsed;
    float delay = 200f;
    int frames = 0;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        destRect = new Rectangle(50, 50, 50, 50);
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        walkRight = Content.Load<Texture2D>("walkRight");
        walkUp = Content.Load<Texture2D>("walkUp");
        walkLeft = Content.Load<Texture2D>("walkLeft");
        walkDown = Content.Load<Texture2D>("walkDown");
        TitleScreen = Content.Load<Texture2D>("TitleScreen");
        stand = Content.Load<Texture2D>("SpriteStill");
        currentWalk = walkRight;
    }

    protected override void UnloadContent()
    {
    }

    private void Animate(GameTime gameTime)
    {
        elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

        if (elapsed >= delay)
        {
            if (frames >= 3)
            {
                frames = 0;
            }
            else
            {
                frames++;
            }
            elapsed = 0;
        }
        sourceRect = new Rectangle(50 * frames, 0, 50, 50);
    }

    protected override void Update(GameTime gameTime)
    //{
        //switch (gameStates)
    {
        if (position.Y >= Window.ClientBounds.Height - 50)
        {
            position.Y = Window.ClientBounds.Height - 50;
            isGrounded = true;
        }
        if (position.X >= Window.ClientBounds.Width - 50)
        {
            position.X = Window.ClientBounds.Width - 50;
        }
        if (position.X <=0)
        {
            position.X = 0;
        }
        if (position.Y <= 0)
        {
            position.Y = 0;
        }

        ks = Keyboard.GetState();

        if (ks.IsKeyDown(Keys.Right))
        {
            position.X += 3f;
            currentWalk = walkRight;
        }
        if (ks.IsKeyDown(Keys.Left))
        {
            position.X -= 3f;
            currentWalk = walkLeft;
        }

        if (ks.IsKeyDown(Keys.Down))
        {
            position.Y += 3f;
            currentWalk = walkDown;
        }

        Animate(gameTime);

        destRect = new Rectangle((int)position.X, (int)position.Y, 50, 50);

        FallManagement(gameTime);

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(currentWalk, destRect, sourceRect, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }

    void FallManagement(GameTime gameTime)
    {
        position.Y += fallSpeed;
        if (isGrounded == false)
        {
            fallSpeed = 5;
        }

        if (ks.IsKeyDown(Keys.Up) && isGrounded)
        {
            fallSpeed -= 40;

            isGrounded = false;
        }
    }
}

I am currently creating a very basic XNA platform game for a year one college course. I am currently trying to add in a statement that says if no keys are pressed then load up texture 2d for the character standing still, but I'm not sure how to check if the there are no keys pressed at all. Basically I would like to check if the character is moving and if not the texture for the character would be set to the standing still png, does anyone know how I might do this. I have put the entire code into this question because I am not using a character class and my level of coding is extremely basic at current.


Solution

  • Maybe something like this would work for you:

    ks = Keyboard.GetState();
    bool isMoving = false;
    
    if (ks.IsKeyDown(Keys.Right))
    {
        position.X += 3f;
        currentWalk = walkRight;
        isMoving = true;
    }
    if (ks.IsKeyDown(Keys.Left))
    {
        position.X -= 3f;
        currentWalk = walkLeft;
        isMoving = true;
    }
    
    if (ks.IsKeyDown(Keys.Down))
    {
        position.Y += 3f;
        currentWalk = walkDown;
        isMoving = true;
    }
    
    if (!isMoving)
    {
        //Do whatever you need to do when the player is still here
    }
    

    Basically set a flag when any key is pressed that you handle, and then use that flag later to do what you need to do when no key is pressed.

    Or, if you want to check that no key is pressed what-so-ever:

    if (ks.GetPressedKeys() == null || ks.GetPressedKeys().Length == 0)
    {
         //No keys pressed at all. (not sure if GetPressedKeys returns null
         //or zero length array, check when in debug then remove one.
    }