Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualmoneal2001

Posted 03 April 2013 - 08:20 PM

you are asking if the down key is down both times back to back, before assigning currentKeyboardState to previousKeyboardState.  so yes it will jump all the way down to the exit button because you are asking about the same keyboardstate both times.  so both will be activated during the same Update.  you should set up the menu items in an array or list have an indexer that moves up or down one with each press.  also move your keyboardstate declerations out of the method.   like this.

int menuIndex = 0;  // place this in your menu fields
menuItem[] menuItems; //place this in your menu fields
KeyboardState currentKeyboardState; //place this in your menu fields
KeybardState previousKeyboardState; //place this in your menu fields

private void MainMenuSelection()
        {            
            currentKeyboardState = Keyboard.GetState();
            
            menuItems[index].IsActive = true;

            // If player presses down arrow go down in menu
            if (currentKeyboardState.IsKeyDown(Keys.Down) && previousKeyboardState.IsKeyUp(Keys.Down))
            {
                   //deactivate current menuItem
                   menuItems[menuIndex].IsActive = false;
 
                   //go down in the menu
                   menuIndex++;
                   
                   // keep index within the your menu items
                   if(menuIndex <= menuItems.Count)
                        index = 0;

                   //activate new menuItem
                   menuItems[index].IsActive = true;

            }

            // If player presses Up Go Up in menu
            if (currentKeyboardState.IsKeyDown(Keys.Up) && previousKeyboardState.IsKeyUp(Keys.Up))
            {
               //deactivate current menuItem
               menuItems[menuIndex].IsActive = false;

              //go up in the menu
              menuIndex--;

              // goes to last menuItem if already at top of menu
              if(menuIndex > 0)
                 index = menuItems.Count -1;

              //activate new menuItem
              menuItems[index].IsActive = true;
            }

            previousKeyboardState = currentKeyboardState;

#1moneal2001

Posted 03 April 2013 - 08:17 PM

you are asking if the down key is down both times back to back, before assigning currentKeyboardState to previousKeyboardState.  so yes it will jump all the way down to the exit button because you are asking about the same keyboardstate both times.  so both will be activated during the same Update.  you should set up the menu items in an array or list have an indexer that moves up or down one with each press.   like this.

int menuIndex = 0;  // place this in your menu fields
menuItem[] menuItems; //place this in your menu fields


private void MainMenuSelection()
        {

            KeyboardState currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();
            
            menuItems[index].IsActive = true;

            // If player presses down arrow go down in menu
            if (currentKeyboardState.IsKeyDown(Keys.Down) && previousKeyboardState.IsKeyUp(Keys.Down))
            {
                   //deactivate current menuItem
                   menuItems[menuIndex].IsActive = false;
 
                   //go down in the menu
                   menuIndex++;
                   
                   // keep index within the your menu items
                   if(menuIndex <= menuItems.Count)
                        index = 0;

                   //activate new menuItem
                   menuItems[index].IsActive = true;

            }

            // If player presses Up Go Up in menu
            if (currentKeyboardState.IsKeyDown(Keys.Up) && previousKeyboardState.IsKeyUp(Keys.Up))
            {
               //deactivate current menuItem
               menuItems[menuIndex].IsActive = false;

              //go up in the menu
              menuIndex--;

              // goes to last menuItem if already at top of menu
              if(menuIndex > 0)
                 index = menuItems.Count -1;

              //activate new menuItem
              menuItems[index].IsActive = true;
            }

            previousKeyboardState = currentKeyboardState;

PARTNERS