我正在用 Xna 用 C# 构建一个游戏。当我在菜单和播放屏幕之间更改游戏状态时,就好像新屏幕刚刚绘制在新屏幕上一样。当我点击前一个屏幕上按钮所在的屏幕部分时,它会改变状态,如果我在第一级并点击第二级按钮在前一个屏幕上的屏幕部分,它会直接跳转到水平2.
这是我第一个菜单按钮类的代码:
class menubutton1
{
public bool selected = false;
public string text = "menuButton";
public Rectangle area;
public menubutton1(Rectangle Area, string Text, int i)
{
area = Area;
text = Text;
i = 4;
}
public void mouseOver(MouseState mbd)
{
//CHECK IF MOUSE IS OVER BUTTON
if (mbd.X > area.X && mbd.X < area.X + area.Width && mbd.Y > area.Y && mbd.Y > area.Y + area.Height)
{
selected = true;
}
else { selected = false; }
}
public void Draw(SpriteBatch spriteBatch, SpriteFont font)
{
if (selected == false)
{
spriteBatch.DrawString(font, text, new Vector2(area.X, area.Y), Color.Black);
}
if (selected == true)
{
spriteBatch.DrawString(font, text, new Vector2(area.X, area.Y), Color.White);
}
}
}
}
这是主要的代码:
List<menubutton1> mbs1 = new List<menubutton1>();
这是来自更新方法:
foreach (menubutton1 m in mbs1)
{
m.mouseOver(mbd);
//Check if play button has been clicked
if (m.text == "PLAY GAME 1" && m.selected == true && mbd.LeftButton == ButtonState.Pressed && prevmbd.LeftButton == ButtonState.Released)
{
gamestate = gameState.play1;
}
if (m.text == "PLAY GAME 2" && m.selected == true && mbd.LeftButton == ButtonState.Pressed && prevmbd.LeftButton == ButtonState.Released)
{
gamestate = gameState.play2;
}
if (m.text == "PLAY GAME 3" && m.selected == true && mbd.LeftButton == ButtonState.Pressed && prevmbd.LeftButton == ButtonState.Released)
{
gamestate = gameState.play3;
}
}
有没有办法阻止这种情况发生?在更改状态以移动到新屏幕之前,有没有办法从上一个屏幕清除按钮?
对此的任何帮助将不胜感激。