0

我正在用 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;
                }



        }

有没有办法阻止这种情况发生?在更改状态以移动到新屏幕之前,有没有办法从上一个屏幕清除按钮?

对此的任何帮助将不胜感激。

4

1 回答 1

0

取决于你想如何设置它。一种方法是使用 GraphicsDevice.Clear(Color.Black); 这会给您留下一个可以绘制的黑屏。另一种方法是创建多个类,并且当它更改 GameStates 时,使其从特定类加载 Draw,该类只会绘制该类中的内容。

于 2013-05-25T01:13:04.193 回答