[.net] question using case: for input (XNA)

Started by
7 comments, last by Dieseltjuh 16 years, 1 month ago
Hi, im developping my own game atm, and im writing the input part at the moment. I've found code to show me how to implement keyboard input:

    KeyboardState oldState = Keyboard.GetState();

    protected override void Update( GameTime gameTime )
    {
        // Allows the default game to exit on Xbox 360 and Windows
        if (GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        UpdateInput();

        base.Update( gameTime );
    }

    private void UpdateInput()
    {
        KeyboardState newState = Keyboard.GetState();

        // Check to see whether the Spacebar is down.
        if (newState.IsKeyDown( Keys.Space ))
        {
            // Key has just been pressed.
        }
        // Otherwise, check to see whether it was down before.
        // (and therefore just released)
        else if (oldState.IsKeyDown( Keys.Space ))
        {
            // Key has just been released.
        }

        oldState = newState;
    }



Now in my game i want to use the spacebar, the up and down arrow, and the ctrl button. So it would be stupid to write if statements for them all, so i want to use the "case:" statement. The only problem is that i dont know what parameter to give it for each button. ("case newState.IsKeyDown(Keys.Space):" doesn't work). Im fairly new at this, but if anyone could help me out with this i'd be very thankfull.
-Please rate me if you find my replies useful.
Advertisement
Are you saying the player is only allowed to use one key at a time? If not then you wouldn't use a switch block, the if statements are the way to go. Also, your check for a key being pressed is slight incorrect. To check for a key just being pressed you would do:

if (newState.IsKeyDown( Keys.Space ) && !oldState.IsKeyDown( Keys.Space )){    // Key has just been pressed.}

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

The way I do it is a little different.

I use a map<> of delegates to the functions. It makes it easier to manage then a monolithic switch or if block, at least to me.

theTroll
Quote:Original post by TheTroll
The way I do it is a little different.

I use a map<> of delegates to the functions. It makes it easier to manage then a monolithic switch or if block, at least to me.

theTroll

Agreed. For someone just starting out though, understanding the basics is probably a good thing. Also, if the game is only 4 keys it's not a big deal IMO. [grin]

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

so eehm the idea is that up and down are not pressed simutaniously, but ctrl and space or keyarrow + space might be pressed simultaniously.

should i stick to the if statements?

maybe some1 can make show an example using case's :)

Thanks for the input guys.
-Please rate me if you find my replies useful.
Using a switch to check the keyboard means that you'll kick out of the switch when it hits the first check that evaluates to true. So if you're checking for firing a weapon first and moving second you'll never hit the moving check if you're firing the weapon. The same thing done with ifs:

            if (newState.IsKeyDown(Keys.Escape) && _oldState.IsKeyDown(Keys.Escape))            {                this.Exit();            }            if (newState.IsKeyDown(Keys.Space) && _oldState.IsKeyDown(Keys.Space))            {                //fire weapon            }            if (newState.IsKeyDown(Keys.Left) && _oldState.IsKeyDown(Keys.Left))            {                //move left            }


You don't have that problem.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

thanks alot, i totally realise that i cant use case now thanks :)

ill give you a hint when the game is done if you like, thanks for the help again all!
-Please rate me if you find my replies useful.
Actually you can continue to use case statements.

Call KeyboardState.GetPressedKeys();
which returns an array of Keys[] which you can parse and run through a switch.

I also use this to convert alot of keys (Keys is a large enum) to their ascii version.

string letter = Keys.A.ToString();

Of course you will have to filter out and catch special keys but it beats having to filter out 105+ keys.


-------------------------------------------------------- What''s the difference between engineers and architects? Engineers build weapons; architects build targets. --------------------------------------------------------
Thanks again, i tryed to use it but i failed :P

anyway, i used the following code to fix it atm:
#region down// Is the down-arrow key down?if (newState.IsKeyDown(Keys.Down)){   fanPosition += moveFactorPerSecond;}else if (oldState.IsKeyDown(Keys.Down)){   // Key was down last update, but not down now, so   // it has just been released.                }#endregion

i realised i used too much code i didn't need :)
-Please rate me if you find my replies useful.

This topic is closed to new replies.

Advertisement