我试图添加退出功能,让玩家退出我的游戏在不同的阶段。我目前有它的设置,使他们可以按下逃生按钮,而在游戏中退出。但是我无法在游戏主菜单或游戏结束菜单中添加相同的功能。
下面是我的简化代码:
// 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和游戏菜单游戏?
谢谢
在其他两种情况下,您不调用HandleInput
。
移动到switch语句的前面