Domanda

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?

È stato utile?

Soluzione

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))

Altri suggerimenti

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))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top