【发布时间】:2015-06-03 11:23:29
【问题描述】:
我一直在开发我的第一个 Xna 游戏,是的,这就是我编码不佳的借口。我已经设法解决了大多数问题,但不是这个。我实在看不下去了。当我的玩家死亡(称为精灵)时,我试图让游戏恢复默认状态。你们能帮忙吗?需要明确的是,当我的玩家死亡时,我希望 GameState 回到 MainMenu,并且按钮恢复正常。现在,当玩家死亡时,无法按下按钮。提前致谢。 我将提供与菜单/按钮有关的所有内容。 在游戏 1 中:
enum GameState
{
MainMenu,
Play,
HighScore,
GameOver,
}
GameState CurrentGameState = GameState.MainMenu;
``
cButtonplay btnPlay;
cButtonHS btnHighScore;
protected override void LoadContent()
{
IsMouseVisible = true;
btnPlay = new cButtonplay(Content.Load<Texture2D>("button_play"), graphics.GraphicsDevice);
btnPlay.setPosition(new Vector2(300, 250));
btnHighScore = new cButtonHS(Content.Load<Texture2D>("button_highscore"), graphics.GraphicsDevice);
btnHighScore.setPosition(new Vector2(300, 300));
}
还有我的更新逻辑:
protected override void Update(GameTime gameTime)
{
//MENU
isSpriteAlive = true;
MouseState mouse = Mouse.GetState();
switch (CurrentGameState)
{
case GameState.MainMenu:
if (btnPlay.isClicked == true)
{
btnPlay.isClicked = false;
CurrentGameState = GameState.Play;
}
btnPlay.Update(mouse);
if (btnHighScore.isClicked == true)
{
btnHighScore.isClicked = false;
CurrentGameState = GameState.HighScore;
// btnHighScore.Update(mouse);
}
break;
case GameState.Play:
isSpriteAlive = true;
// Here is alot of various code
case GameState.HighScore:
if (btnHighScore.isClicked == true) CurrentGameState = GameState.HighScore;
btnHighScore.Update(mouse);
break;
case GameState.GameOver:
btnHighScore.isClicked = false;
btnHighScore.Update(mouse);
break;
最后是我的绘制方法:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.ForestGreen);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null);
//MENU
switch (CurrentGameState)
{
case GameState.MainMenu:
spriteBatch.Draw(Content.Load<Texture2D>("menu_background"), new Rectangle(0, 0, screenWidth, screenHeight), Color.WhiteSmoke);
btnPlay.playDraw(spriteBatch);
btnHighScore.highScoreDraw(spriteBatch);
printText.PrintMainMenu("By: Felix Claesson\nand Carl Wikstrom", spriteBatch, 250, 500);
//printText.Print("$ " + points * 25, spriteBatch, 0, 0);
break;
case GameState.Play:
if (isSpriteAlive == true)
{
spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
foreach (Enemies enemy in enemies)
enemy.Draw(spriteBatch);
foreach (Bullets bullet in bullets)
bullet.Draw(spriteBatch);
foreach (healthBars healthbar in healthbars)
healthbar.Draw(spriteBatch);
if (isSpriteAlive == true)
spriteBatch.Draw(spriteTexture, spritePosition, new Rectangle(56 * frames, 0, 56, 51), Color.White, playerRotation, spriteOrigin, 1f, SpriteEffects.None, 0);
printText.Print("$ " + points * 25, spriteBatch, 0, 0);
}
else CurrentGameState = GameState.GameOver;
break;
case GameState.HighScore:
printText.Print(":-)", spriteBatch, 350, 300);
break;
case GameState.GameOver:
printText.Print("h", spriteBatch, 350, 300);
btnHighScore.highScoreDraw(spriteBatch);
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
}
您有什么建议吗? 我不能玩两次,我的按钮也不能用两次。 最好的问候。
【问题讨论】:
-
cButtonplay 和 cButtonHS 是您自己的课程吗?您可以为他们粘贴代码吗?另外,按钮 .isClicked 属性在哪里设置为 true?
-
嗯,很难理解提供的代码的问题...我知道健康条是玩家健康条的集合?如果是这样,则在您的更新方法中,“IsSpriteAlive”(这是玩家吗?)应设置为一个值,具体取决于所有生命条是否为 0。有了这个设置,如果他不活着,那么只需更改游戏状态值。