我不确定我的按钮类文件是否只适用于一个GameState

本文关键字:GameState 一个 适用于 我的 不确定 按钮 是否 文件 | 更新日期: 2023-09-27 18:16:07

这是我创建按钮的类文件:

class Button
{
    Texture2D buttonTexture;
    Rectangle buttonRectangle;
    Color buttonColour = new Color(255, 255, 255, 255);
    public Vector2 size, buttonPosition;
    public Button(Texture2D newButtonTexture, Vector2 newSize)
    {
        buttonTexture = newButtonTexture;
        size = newSize;
    }
    public void setPosition(Vector2 newButtonPosition)
    {
        buttonPosition = newButtonPosition;
    }
    bool down;
    public bool isClicked;
    public void Update(MouseState mouse)
    {
        buttonRectangle = new Rectangle((int)buttonPosition.X, (int)buttonPosition.Y, (int)size.X, (int)size.Y);
        Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
        if (mouseRectangle.Intersects(buttonRectangle))
        {
            if (buttonColour.A == 255)
                down = false;
            if (buttonColour.A == 0)
                down = true;
            if (down)
                buttonColour.A += 3;
            else
                buttonColour.A -= 3;
            if (mouse.LeftButton == ButtonState.Pressed)
                isClicked = true;
        }
        else if (buttonColour.A < 255)
        {
            buttonColour.A += 3;
            isClicked = false;
        }
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(buttonTexture, buttonRectangle, buttonColour);
    }
}

下面是我在LoadContent中创建按钮的方法(忽略那些蹩脚的参数,这只是我目前的做法):

btnResume = new Button(Content.Load<Texture2D>("Buttons/button_Resume"), new Vector2(GraphicsDevice.Viewport.Width / 1.875f, GraphicsDevice.Viewport.Height / 4));
btnResume.setPosition(new Vector2(GraphicsDevice.Viewport.Width / 2 - (0.5f*btnResume.size.X), GraphicsDevice.Viewport.Height / 3));
btnQuit = new Button(Content.Load<Texture2D>("Buttons/button_Exit"), new Vector2(GraphicsDevice.Viewport.Width / 1.875f, GraphicsDevice.Viewport.Height / 4));
btnQuit.setPosition(new Vector2(GraphicsDevice.Viewport.Width / 2 - (0.5f*btnQuit.size.X), GraphicsDevice.Viewport.Height - (GraphicsDevice.Viewport.Height / 3)));

这是我在Game1.cs的绘制函数中绘制两个按钮的方法:

case GameState.Menu:
    btnResume.Draw(spriteBatch);
    btnQuit.Draw(spriteBatch);
    break;

这一切都很好。然而,出于某种原因,当对"播放"按钮做同样的事情,把一个新的游戏状态称为"主菜单",按钮不显示(我已经测试过,把按钮代码在游戏状态"菜单"和按钮确实出现,它只是不会显示在任何其他游戏状态除了"菜单")。

有人知道为什么它不起作用吗?我记得创建按钮,设置位置,然后在"case GameState"中绘制它。"主菜单"是Game1.cs中绘制功能的一部分,所以我真的不知道为什么它不起作用。作为旁注,我曾尝试为另一种称为"CharacterSelection"的游戏状态绘制按钮,但这也不起作用,但如果我使用spriteBatch,它就起作用了。绘制(所以我很确定这是一个问题与按钮类的绘制功能。

我可以提供代码或游戏元素的截图,如果代码很混乱,我很抱歉,我倾向于这样做很多。这是我的第一款XNA游戏,我以前很少用c#编写代码,所以很多东西对我来说都是新的。

编辑:
这是我所有的游戏:

enum GameState 
{ 
    Opening, 
    MainMenu, 
    CharacterSelection, 
    Countdown, 
    Playing, 
    Menu, 
    Options,
} 
GameState CurrentGameState = GameState.Opening;

打开-打开电影主菜单-标题屏幕基本上角色选择-选择什么角色玩。倒计时- 3,2,1,游戏开始前。

我不确定我的按钮类文件是否只适用于一个GameState

菜单-暂停菜单(按esc时打开)选项-在暂停或主菜单中按"选项"时(尚未设置)

谢谢你的帮助,但我已经解决了。我犯了一个愚蠢的错误!在我的更新函数中忘记了"btnPlay.Update(鼠标);"!__ - - - - - -

相关文章: