我正在用Xna在C#中构建游戏。当我在菜单和播放屏幕之间更改游戏状态时,就好像新屏幕刚刚在新屏幕上绘制一样。当我点击屏幕上前一个屏幕上按钮所在的部分时,它会改变状态,如果我在第一级并点击屏幕上前一个屏幕上的2级按钮,它会直接跳到水平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;
}
}
反正有没有阻止这种情况发生?在更改状态以移动到新屏幕之前,有没有办法清除上一屏幕中的按钮?
对此的任何帮助将不胜感激。
答案 0 :(得分:0)
取决于您想要如何设置它。 一种方法是使用GraphicsDevice.Clear(Color.Black);哪个会留下你可以画的黑屏。另一种方法是创建多个类,当它发生变化时,GameStates会使它从特定的类中加载一个Draw,它只会绘制该类中的内容。