Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In my CheckKeyboard(Keys.Enter) it does both of the things when I only want it to do one and the the other on the next enter click, I can't think of how to fix it. Thanks in Advanced, here is the related code;

enum CurrentState
{
    Menu,
    NewGame,
    LevelSelect
}
private CurrentState _currentState = CurrentState.Menu;
public void Update(GameTime gameTime, Game1 game)
{
    keyboard = Keyboard.GetState();
        switch (_currentState)
            {
                //Error-  The name 'Menu' does not exist in the current context.
                case Menu:
                {
                }
            }
    prevKeyboard = keyboard;
}
public bool CheckKeyboard(Keys key)
{
    return (keyboard.IsKeyUp(key) && prevKeyboard.IsKeyDown(key));
}
share|improve this question

2 Answers 2

up vote 0 down vote accepted

In this case a 'state machine' system would be an alternative. (This way you can enhance it later more easily than using different flags).

First, define an enum with the possible states:

enum CurrentState {
     ShowOptions,
     SelectLevel
}

Then, define a field inside the current class, and initialise it with ShowOptions:

private CurrentState _currentState = CurrentState.ShowOptions;

In the Update method you'll have something like this:

switch(_currentState) {
    case CurrentState.ShowOptions:
    {
        if (CheckKeyboard(Keys.Enter))
        {
            if (selected == buttonList.Count - 2)
            {
                _currentState = CurrentState.SelectLevel;
            }
        }
        break;
    }
    case CurrentState.SelectLevel:
    {
        if (CheckKeyboard(Keys.Enter))
        {
            if (sChapter == chapterList.Count - 1) sChapter1 = "C3";
            if (sChapter == chapterList.Count - 2) sChapter1 = "C2";
            if (sChapter == chapterList.Count - 3) sChapter1 = "C1";

            // todo: advance to another state (maybe start the actual game?)
        }
        break;
    }           
}

It can be seen clearly how the states flow and go one after another. This allows you to have complex scenarios where you would need to go back to a state. For every new state, you just add an enum member and handle it inside the switch.

share|improve this answer
    
I added Menu to the Enum and now when I am trying to do case Menu: it is not working and says Menu doesn't exist in the current context, why does it say that? –  TheQuantumBros May 27 '13 at 18:17
    
Could you paste the code in pastebin.com ? It should work if you added the member the right way in the enum. –  Timotei May 27 '13 at 19:20
    
Well I don't want to give all of my code away so what is being used I will add to the question –  TheQuantumBros May 27 '13 at 22:04
    
I added what it is now. –  TheQuantumBros May 27 '13 at 22:06
    
Ah, OK. I am sorry, I typed the code without actually compiling it. You should haven't just copy it. Anyway, I've fixed it. I missed the "CurrentState." before each case statement. Also don't forget to break; at the end of each case. I've updated the code sample in my answer. –  Timotei May 27 '13 at 23:07

This is one possible solution for the code given. Definitely not the best and I see a couple of other routes, but it should provide enough hints to make better.

Start by giving the class that contains this Update() Method a member variable.

private bool _isLevelSelect;

remove these from your current CheckKeyboard(Keys.Enter) implementation

if (selected == buttonList.Count - 2) Selected = "LevelSelect";

if (sChapter == chapterList.Count - 1) sChapter1 = "C3";
if (sChapter == chapterList.Count - 2) sChapter1 = "C2";
if (sChapter == chapterList.Count - 3) sChapter1 = "C1";

add

if (CheckKeyboard(Keys.Enter) && selected == buttonList.Count - 2)
{
    Selected = "LevelSelect";
    _isLevelSelect = true;
}

if (CheckKeyboard(Keys.Enter) && _isLevelSelect)
{
    if (sChapter == chapterList.Count - 1) sChapter1 = "C3";
    if (sChapter == chapterList.Count - 2) sChapter1 = "C2";
    if (sChapter == chapterList.Count - 3) sChapter1 = "C1";
}

provide a way for user to cancel that selection

if (CheckKeyboard(Keys.Escape) && _isLevelSelect)
{
   _isLevelSelect = false;
} 
share|improve this answer
    
It is still doing the same thing as it was. –  TheQuantumBros May 27 '13 at 18:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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