我正在尝试添加退出功能,让玩家在不同阶段退出游戏。我目前已经进行了设置,以便他们可以在游戏中按退出按钮退出。但是我无法在游戏主菜单或游戏菜单中添加相同的功能。
这是我的代码简化:
// 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游戏状态下,如何让玩家退出游戏?
由于
答案 0 :(得分:0)
在其他两种情况下,你没有打电话给HandleInput
。
将其移至switch语句之前。