So I need a c# if() statement which should look something like this, maybe? Code:

...
if(CurrentGameState != gameState.gameLoading && gameState.mainMenu && keyboardState.IsKeyDown(Keys.Escape))
{
      CurrentGameState = gameState.mainmenu;

}
//I get error in: CurrentGameState != gameState.gameLoading && gameState.mainMenu part
...

And that's the code I have used the variables above from:

    //Game States
    enum gameState
    {
        gameLoading,
        mainMenu,
        gameOptions,
        levelSelect,
        gamePlaying,
        gameOver
    }
    gameState CurrentGameState = gameState.gameLoading;
    //Keyboard State
    KeyboardState keyboardState = Keyboard.GetState();

Any suggestions?

有帮助吗?

解决方案

gameState.mainMenu is an enum value, not a boolean value. You can't use it the way you're trying to. Did you mean to do this?

if (CurrentGameState != gameState.gameLoading && 
    CurrentGameState != gameState.mainMenu &&
    keyboardState.IsKeyDown(Keys.Escape))

其他提示

I appologise for formatting but writing from my phone, but I presume your working with xna, you should have your game states in a switch statement otherwise your code will get messy very fast as well as inefficient

 Switch(currentgamestate)
 {
      Case gamestate.loading:
          If(pressing escape)
              Currentgamestate = gamestate.mainmenu;
       Break;
       Case gamestate.mainmenu;
            //todo
        Break;
   }

Your error is because you are trying to link two conditions together ..if currentgamestate isnt loading and is main menu, you need to reference current gamestate for each condition.. if currentgamestate isn't loading and currentgamestate is mainmenu

if(CurrentGameState != gameState.gameLoading 
   && gameState.mainMenu && keyboardState.IsKeyDown(Keys.Escape))

may be

if(CurrentGameState != gameState.gameLoading && 
   CurrentGameState==gameState.mainMenu && keyboardState.IsKeyDown(Keys.Escape))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top