Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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--;
            }
        }
share|improve this question
    
I've edited the code to manage the keyboard –  Blau Aug 31 '13 at 19:36

1 Answer 1

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--;
          }
   }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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