XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

How do i add input delay when scrolling through a menu selection?

Last post 2/11/2009 10:03 PM by Carradon. 3 replies.
  • 2/11/2009 8:08 PM

    How do i add input delay when scrolling through a menu selection?

    Heya guys, i have a titlescreen in my game where i want to be able to scroll through the menu selection. (New Game, Options, Exit Game) without having to repeately press the up/down keyboard keys to scroll through the different menu selections. The problem im having, is that it is scrolling through the options way too fast, & i'm not sure how to add in a timer to the up/down input so that it scrolls through (New Game, Options, and Exit Game) at a reasonable pace. like say waiting 2/10th's of a second before scrolling through the next menu option. How would i go about doing this?
  • 2/11/2009 8:14 PM In reply to

    Re: How do i add input delay when scrolling through a menu selection?

    This stuff is a lot easier to do if you wrap the input functions in a higher level class. Then you can check for NewPress() or BeingHeld() etc... Store a timer value, and have a function only return true if a certain amount of time has passed, than reset the timer value. Then you can hold the stick in a direction, and only have the cursor move at a comfortable rate.
    return;
  • 2/11/2009 8:23 PM In reply to

    Re: How do i add input delay when scrolling through a menu selection?

    EDIT: Haha, simul-posted. My post is just a long-winded elaboration of Daaark's post.

    Basically, you're going to need a timer variable for every key/button that you'll want to allow for this time delay. What I've done is created an InputState struct that looks like this:

    struct InputState
    {
       public bool Previous; // True if button was pressed last frame
       public bool Current;  // True if button is pressed this frame
       public bool Pressed; // True for each frame of delayed input
       public int Timer;     // Number of milliseconds before next delayed input
    }

    Then, I created an Input class that has an InputState for every key/button. I also declare two constant values, REPEAT_DELAY and REPEAT_INTERVAL. REPEAT_DELAY is the number of milliseconds between the initial button press and the first repeat, and REPEAT_INTERVAL is the number of milliseconds between each repeated input. On each Update call to the game, I update every InputState in the Input class like this:

    inputState[button].Previous = inputState[button].Current;
    inputState[button].Current = gamePadState.IsButtonDown(button);
    
    if (inputState[button].Current)
    {
       if (inputState[button].Previous)
       {
          inputState[button].Timer -= (int)gameTime.ElapsedGameTime.TotalMilliseconds;
          
          if (inputState[button].Timer < 0)
          {
             inputState[button].Pressed = true;
             inputState[button].Timer += REPEAT_INTERVAL;
          }
          else
          {
             inputState[button].Pressed = false;
          }
       }
       else
       {
          inputState[button].Pressed = true;
          inputState[button].Timer = REPEAT_DELAY;
       }
    }
    else
    {
       inputState[button].Pressed = false;
       inputState[button].Timer = 0;
    }

    Now, just check inputState[button].Pressed if you want to detect delayed repeated input, or simply check inputState[button].Current if you want to see if the button is being held down. Hopefully, this will give you a rough idea of how I go about solving this problem.

    Previously known as "Rainault".
    Twitter - me, Jade Vault Games
    Announcing ASCII Quest, a Roguelike under development for Xbox LIVE Indie Games
  • 2/11/2009 10:03 PM In reply to

    Re: How do i add input delay when scrolling through a menu selection?

    thanx a bunch. i got it working perfectly
Page 1 of 1 (4 items) Previous Next