0
\$\begingroup\$

I have a menu that you can navigate with the keyboard, but I also want it so you can navigate it with the controller thumb sticks. But my problem is that if you push down or up on the thumb sticks it will just fly through the menu. To fix problem if it was a keyboard you would just do: prevKeyState.IsKeyUp(Keys.Down). So I was wondering if there anything like this for the controller thumb sticks?

I'm not sure if I'm doing something wrong but it still flies through the menu.

 private enum GamePadStates 
    {
        None,
        GoingDown,
        GoingUp
    }

    GamePadStates CurrentGamePad, PreviousGamePad = GamePadStates.None;  
     public void Update(GameTime gameTime) 
    {
        keyState = Keyboard.GetState();
        gamePadState = GamePad.GetState(PlayerIndex.One);
        PreviousGamePad = CurrentGamePad;

        if (gamePadState.ThumbSticks.Left.Y > 0.1)
            CurrentGamePad = GamePadStates.GoingUp;
        else if (gamePadState.ThumbSticks.Left.Y < -0.1)
            CurrentGamePad = GamePadStates.GoingDown;
        else
            CurrentGamePad = GamePadStates.None;

        if (keyState.IsKeyDown(Keys.Down) && prevKeyState.IsKeyUp(Keys.Down) ||
             CurrentGamePad == GamePadStates.GoingDown && PreviousGamePad != GamePadStates.GoingUp)
        {
            if (selected < (buttonList.Count - 1))
            {
                selected++;
            }
        }

        if (keyState.IsKeyDown(Keys.Up) && prevKeyState.IsKeyUp(Keys.Up) ||
            CurrentGamePad == GamePadStates.GoingUp && PreviousGamePad != GamePadStates.GoingDown) 
        {
            if (selected > 0) 
            {
                selected--;
            }
        }
\$\endgroup\$
  • \$\begingroup\$ I've edited the code to manage the keyboard \$\endgroup\$ – Blau Aug 31 '13 at 19:36
2
\$\begingroup\$

You can do it in a similar way to keyboard, storing the state by yourself:

   enum States {None, GoingDown, GoingUp }

   States Current, Previous;

   void Update()
   {
           Previous = Current;

           if (thumbStiscks.Y>0.5 || keyState.IsKeyDown(Keys.Up))
               State = States.GoingUp;
           else if (thumbSticks.Y<-0.5f || keyState.IskeyDown(Keys.Down)) 
               State = States.GoingDown;
           else State = States.None;

           if ( Current == States.GoingDown  && Previous != States.GoingDown) 
                && selected < (buttonList.Count - 1)) {
             selected++;
           }
           else if ( Current == States.GoingUp && Previous != States.GoingUp) 
                     && selected >0) {
             selected--;
          }
   }
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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