# C# XNA - How to create a menu with bools and textures?

I thought I could use bools to be able to select options in my menu. But the options should be textures. To show that an optino is selected I have 2 textures like this:

[----Menu Screen----]
[  >  Option1  <  ] Option1 = Start Game
[     Option2     ] Option2 = Options
[     Option3     ] Option3 = Exit


So the > and < are the textures I'm using to show the user that he has selected an option. I have the following code:

enum gameStates
{
gameOptions,
levelSelect,
gamePlaying,
gameOver,
}


And maybe using bools like this may work?

// gameStates.mainMenu bools
bool startGameSelected = true;
bool optionsSelected = false;
bool exitSelected = false;


Update() method:

    switch (CurrentGameState)
{

if (startGameSelected)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
CurrentGameState = gameStates.levelSelect;
}
}
if (optionsSelected)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
CurrentGameState = gameStates.gameOptions;
}
}
if (exitSelected)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
this.Exit();
}
}
if (startGameSelected)
{
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
exitSelected = true;
optionsSelected = false;
startGameSelected = false;
}

if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
startGameSelected = false;
exitSelected = false;
optionsSelected = true;
}
}
if (optionsSelected)
{
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
startGameSelected = true;
exitSelected = false;
optionsSelected = false;
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
exitSelected = true;
optionsSelected = false;
startGameSelected = false;
}
}
if (exitSelected)
{
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
optionsSelected = true;
exitSelected = false;
startGameSelected = false;
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
startGameSelected = true;
exitSelected = false;
optionsSelected = false;
}
}


Draw() method:

switch (CurrentGameState)
{

// Draw the startGame button
spriteBatch.Draw(startGameTexture, startGamePosition, Color.White);
// Draw the exitGame button
spriteBatch.Draw(exitGameTexture, exitGamePosition, Color.White);
// Draw the gameOptions button
spriteBatch.Draw(gameOptinonsTexture, gameOptionsPosition, Color.White);
// Draw the ">" and "<" arrows
if (startGameSelected)
{
spriteBatch.Draw(redArrowTexture, new Vector2(280, 320), Color.White);
spriteBatch.Draw(greenArrowTexture, new Vector2(484, 320), Color.White);
}


But for some strange reason it seems this won't work any suggestions?

Given that only one item can be selected at a time, it would be better to store the index of the selected item (possibly as an enum, for clarity - you can cast between an enumeration and an int), rather than using a mess of Booleans.

That way you can:

• Move through the menu with ++ and --
• Detect that the selected item has gone out of range, use the % operator to implement wrapping
• Use a select statement to select an appropriate action to take when activating the selected menu item
• Use a single Draw statement to draw your selection cursor textures, use simple maths to calculate their position based on which item is selected
• Thanks for the answer @Andrew Russell! I'd prefer enum. But anyway could I have an example code? Then I'll mark this as the answer of this question. – PowerUser Mar 11 '14 at 12:58
• I'd rather not - this is a Q&A site, not a "write my code for me" site, after all. The code is fairly trivial, though, so why don't you give it a go? If you have any trouble, feel free to ask a new question. – Andrew Russell Mar 11 '14 at 14:24
• OK so something like this: I create the enum and then I put its switch statemant in the CurrentGameState switch, case gameStates.mainMenu in the Update() and Draw() method? And maybe use if() statements for the loginc? Also you're right I just need to plan the code in my mind and when I type it everything should be alright. If I succeed I'll mark this as the answer. – PowerUser Mar 11 '14 at 14:52
• @Charlie Use switch(selectedMenuItem) { ... } where you need to. But try and use maths like new Vector2(xOffset, yOffset + (int)selectedMenuItem * lineHeight) where possible. (Obviously these go with something like enum MenuItem { ... } and MenuItem selectedMenuItem;.) – Andrew Russell Mar 12 '14 at 13:24
• Done. Used if() statements again to add the Update() logic but the when changing Start Game to Options it directly flips to Exit. Just one more suggestion and I accept this answer. – PowerUser Mar 13 '14 at 19:39