Reputation:
I need to make a menu in my game. I did it but if I want to enter inside "Options" section I can't go back to main menu.First of all I drew the basic images on the screen (play,options and exit), if the counter is for example 0 i draw another image with different effects above the another one. Look here(I didn't rewrite all the code):
int count=0;
KeyboardState state=Keyboard.GetState();
if(count <6)//main menu
{
//if i press "UP" key count --
//if I press "Down" key count++
//and i did:
if(count==0)//and I press "enter"
{
//Play
}
if(count==1)//and I press "enter"
{
// count =6 and go to the opstions
}
if(count==2)//and I press "enter"
{
//Exit
}
//This functions!
}
//now i am in Options section
if(count>5)
{
//here I have some options including "back button" which is number 8
if(count==8)
{
if(Keyboard.GetState().IsKeyDown(Keys.Enter) && state.IsKeyUp(Keys.Enter))
{
count=1;//and I should move in Main menu BUT IT DOESN'T FUNCTION!! WHY?
}
state=Keyboard.GtState();
}
}
Upvotes: 0
Views: 1045
Reputation: 23
I have some recommendations for improvement to your input stuff first that you might find useful. I am running off the assumption that this is an update function since you seem to grab the state first and use it throughout (please, do let me know if i'm wrong).
To check and see if a key is pressed once (not held), you can achieve that by storing two KeyboardStates as you can see below.
// You need to store these in your class, not locally
KeyboardState m_PreviousState;
KeyboardState m_CurrentState;
Next you will want to put this in your update function:
// At the start of the function, put this
m_CurrentState = Keyboard.GetState();
// do whatever your update function is supposed to do
// At the end of the function, put this
m_PreviousState = m_CurrentState;
What this does is grabs the current state each frame so that you know what the keyboard is currently like. And by storing the state from the last frame in m_PreviousState, you can now check and see if a key was pressed. To do this, you would want to do this:
if (m_CurrentState.IsKeyUp(Keys.Enter) && m_PreviousState.IsKeyDown(Keys.Enter))
{ /* Run whatever logic */ }
This will solve your key pressed problem. As for your menu, I recommend using enums to track your progress in the menu so to speak. Let me layout a basic menu and how you could go about doing it programmatically.
Play
Load Save
Options
- Mute Sound
- Mute Volume
- Raise / Lower Volume
Quit
Let us assume that this is your menu (please fill it in on your end with whatever your menu is). I would store the main menu (Play, Load Save, Options and Quit) in an enum of its own and I would store the stuff in the options submenu (Mute Sound, Mute Volume, Raise/Lower Volume) it is own enum as well. This can be done as seen below.
// Putting MAX and MIN in both of them will be useful later, just trust me
public enum MenuOptions
{
MIN = -1,
PLAY,
LOAD_SAVE,
OPTIONS,
QUIT,
MAX
}
public enum OptionsOptions // Yes, i know its not the best name
{
MIN = -1,
MUTE_SOUND,
MUTE_VOLUME,
RAISE_LOWER_VOLUME,
MAX
}
// Create your variables in your class at the top
private MenuOptions m_CurrentMenuOption;
private OptionsOptions m_CurrentOptionsOption;
private Bool m_InOptions; // We will need this shortly
Now that we have created an enum for each menu and submenu and the variables we are using to store them, we now have an easy to read way of keeping track of where in each menu we currently are. Now for the part where we combine them.
I typically do my key input using event handlers so that it can be run separately from the update function, but i'll run off the assumption that you want to use the update function. Here is how you might go about doing it.
// If Enter was just pressed, move into the submenu if it exists
if (m_CurrentState.IsKeyUp(Keys.Enter) && m_PreviousState.IsKeyDown(Keys.Enter))
{
// If we are currently hovering over options, we should move into that submenu
if (m_CurrentMenuOption == MenuOptions.OPTIONS)
{
// Make sure to reset all other bools like this if you have any to avoid
// confusing bugs later
m_InOptions = true;
}
}
// If Down was pressed, move the enum value we store
else if (m_CurrentState.IsKeyUp(Keys.Down) && m_PreviousState.IsKeyDown(Keys.Down))
{
// Check to see if we are in options submenu so we can move that value rather than
// the main menu's value
if (m_InOptions)
{
m_CurrentOptionsOption++;
// Checks to see if you went further than the last option and sets it to the
// last option if you did
if ((int)m_CurrentOptionsOption >= (int)OptionsOptions.MAX)
m_CurrentOptionsOption = OptionsOptions.RAISE_LOWER_VOLUME;
}
else // You could add some else ifs before for other sub menus
{
// Since we are not in a submenu, update the current menu option
m_CurrentMenuOptions++;
// Checks to see if you went further than the last option and sets it to the
// last option if you did
if ((int)m_CurrentMenuOptions >= (int)MenuOptions.MAX)
m_CurrentMenuOption = MenuOptions.QUIT;
}
}
// If Up was pressed, move the enum value we store
else if (m_CurrentState.IsKeyUp(Keys.Up) && m_PreviousState.IsKeyDown(Keys.Up))
{
// Check to see if we are in options submenu so we can move that value rather than
// the main menu's value
if (m_InOptions)
{
m_CurrentOptionsOption--;
// Checks to see if you went further than the first option and sets it to the
// first option if you did
if ((int)m_CurrentOptionsOption <= (int)OptionsOptions.MIN)
m_CurrentOptionsOption = OptionsOptions.MUTE_SOUND;
}
else // You could add some else ifs before for other sub menus
{
// Since we are not in a submenu, update the current menu option
m_CurrentMenuOptions--;
// Checks to see if you went further than the first option and sets it to the
// first option if you did
if ((int)m_CurrentMenuOptions <= (int)MenuOptions.MIN)
m_CurrentMenuOption = MenuOptions.PLAY;
}
}
// If Backspace was pressed, move back a menu if possible
else if (m_CurrentState.IsKeyUp(Keys.Back) && m_PreviousState.IsKeyDown(Keys.Back))
{
if (m_InOptions) // you would tack on some || or else ifs for any other submenus
{
m_InOptions = false;
}
}
With this bit of code, here is basically what you are doing:
I hope this clarifies how you can go about doing your menu stuff. The only code i left out is your draw function. Just figure out how to draw what the current menu is and you'll be fine. Best of luck!
Upvotes: 2
Reputation: 3360
You can not check this:
if(Keyboard.GetState().IsKeyDown(Keys.Enter) && state.IsKeyUp(Keys.Enter))
It will always be false because a key can not be down and up at the same time, when you have declared
KeyboardState state=Keyboard.GetState();
Why use this:
Keyboard.GetState().IsKeyDown(Keys.Enter)
instead of this
state.IsKeyDown(Keys.Enter)
Upvotes: 1