2
\$\begingroup\$

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
[----Menu Screen----]

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
{
    gameLoading,
    mainMenu,
    gameOptions,
    levelSelect,
    gamePlaying,
    gameOver,
}
gameStates CurrentGameState = gameStates.gameLoading;

And maybe using bools like this may work?

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

Update() method:

    switch (CurrentGameState)
        {
    // gameStates.gameLoading here...

    // gameStates.mainMenu:
            case gameStates.mainMenu:
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)
        {
    // gameStates.gameLoading here...

    // gameStates.mainMenu:
            case gameStates.mainMenu:
    // 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?

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – PowerUser Mar 11 '14 at 12:58
  • \$\begingroup\$ 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. \$\endgroup\$ – Andrew Russell Mar 11 '14 at 14:24
  • \$\begingroup\$ 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. \$\endgroup\$ – PowerUser Mar 11 '14 at 14:52
  • \$\begingroup\$ @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;.) \$\endgroup\$ – Andrew Russell Mar 12 '14 at 13:24
  • \$\begingroup\$ 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. \$\endgroup\$ – PowerUser Mar 13 '14 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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