2013-05-01 23 views
0

我試圖添加退出功能讓玩家在各個階段退出我的遊戲。我現在已經設置了它,以便在遊戲中按下退出按鈕退出。但是我們無法在遊戲主菜單或遊戲菜單中添加相同的功能。無法在菜單狀態下退出遊戲

這裏是我的代碼簡化:

// Game State Improvment 
     // An enumeration for the different states in the game 
     public enum GameState 
     { 
      GameMenu = 0, 
      GamePlay = 1, 
      GameOver = 2 
     } 
     // A variable to hold the current state 
     GameState currentGameState; 

/// <param name="gameTime">Provides a snapshot of timing values.</param> 
     protected override void Update(GameTime gameTime) 
     { 
      switch (currentGameState) 
      { 

       case GameState.GameMenu: 
        // commented in order to start the game without a menu 
        if (GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Enter)) 
        { 
         levelIndex = 0; 
         LoadNextLevel(); 
         level.Player.PlayerLives = 3; 
         currentGameState = GameState.GamePlay; 

        } 
        break; 

       case GameState.GameOver: 
        if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Enter)) 
        { 

         levelIndex = 0; 
         LoadNextLevel(); 
         level.Player.PlayerLives = 3; 
         currentGameState = GameState.GamePlay; 
        } 
        //Add your game over update code here... 
        break; 

       case GameState.GamePlay: 
        // Handle polling for our input and handling high-level input 
        HandleInput(); 

        // update our level, passing down the GameTime along with all of our input states 
        level.Update(gameTime, keyboardState, gamePadState, touchState, 
           accelerometerState, Window.CurrentOrientation); 
        break; 
      } 

      // If the ESC key is pressed, skip the rest of the update. 
      if (exitKeyPressed() == false) 
      { 
       base.Update(gameTime); 

      } 

     } 

     bool exitKeyPressed() 
     { 
      // Check to see whether ESC was pressed on the keyboard or BACK was pressed on the controller. 
      if (keyboardState.IsKeyDown(Keys.Escape) || gamePadState.Buttons.Back == ButtonState.Pressed) 
      { 
       Exit(); 
       return true; 
      } 
      return false; 
     } 

protected override void Draw(GameTime gameTime) 
     { 
      // Game State Improvment 
      switch (currentGameState) 
      { 
       case GameState.GameMenu: 
        //// Start drawing 
        spriteBatch.Begin(); 

        spriteBatch.Draw(mainBackGround, Vector2.Zero, Color.Green); 

        //Stop drawing 
        spriteBatch.End(); 
        break; 

       case GameState.GameOver: 
        //// Start drawing 
        spriteBatch.Begin(); 

        //graphics.GraphicsDevice.Clear(Color.Green); 
        spriteBatch.Draw(gameOverBackGround, Vector2.Zero, Color.Green); 

        //// Stop drawing 
        spriteBatch.End(); 
        break; 

       case GameState.GamePlay: 
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue); 
        spriteBatch.Begin(); 
        level.Draw(gameTime, spriteBatch); 
        DrawHud(); 
        spriteBatch.End(); 
        break; 
      } 

      base.Draw(gameTime); 
     } 

我怎樣才能使人們有可能爲玩家退出遊戲的GAMEOVER和GameMenu gamestates時?

感謝

回答

0

你不是要求在另外兩起案件HandleInput

將它移動到switch語句之前。