【问题标题】:XNA button click not firedXNA 按钮单击未触发
【发布时间】:2016-05-10 11:00:55
【问题描述】:

我创建了一个按钮类,并在主游戏类中调用它。但它没有开火。实际上,当我单击鼠标左键时,我想触发 Button 类内部的 Update 方法。我正在通过 Debug.WriteLine 检查所有鼠标事件,一切正常。有什么问题?

GameBase 类

public class GameBase : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    Button btn;

    public GameBase()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
        graphics.PreferredBackBufferWidth = 600;
        graphics.PreferredBackBufferHeight = 400;
    }
    protected override void Initialize()
    {
        base.Initialize();
    }
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        btn = new Button(Content.Load<Texture2D>("Sprites/Button"), new Vector2(150, 150));
    }
    protected override void UnloadContent()
    {
    }
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        MouseState mouse = Mouse.GetState();
        //Debug.WriteLine(mouse);

        if (btn.Clicked == true)
        {
            Debug.WriteLine("Clicked");
            currentState = GameState.Playing;
            btn.Update(mouse);
        }
        base.Update(gameTime);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(Content.Load<Texture2D>("Sprites/StartBg"), new Rectangle(0, 0, _screenWidth, _screenHeight), Color.White);
        btn.Draw(spriteBatch);
        spriteBatch.End();
        base.Draw(gameTime);
    }
}

按钮类

public class Button
{
    public bool Clicked { get; set; }
    private Texture2D _image;
    private Rectangle _rectangle, _mouseRectangle;
    private Vector2 _coordinate;
    private Color _color;
    private bool _down;

    public Button(Texture2D image, Vector2 coordinate)
    {
        _image = image;
        _coordinate = coordinate;
    }
    public void Update(MouseState mouse)
    {
        mouse = Mouse.GetState();
        _rectangle = new Rectangle((int)_coordinate.X, (int)_coordinate.Y, _image.Width, _image.Height);
        _mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);

        if (_mouseRectangle.Intersects(_rectangle))
        {
            if (_color.A == 255)
                _down = false;
            if (_color.A == 0)
                _down = true;
            if (_down)
            {
                _color.A += 3;
            }
            else
            {
                _color.A -= 3;
            }
            if (mouse.LeftButton == ButtonState.Pressed)
            {
                Clicked = true;
            }
            else if (_color.A < 255)
            {
                _color.A += 3;
                Clicked = false;
            }
        }
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(_image, _coordinate, Color.White);
    }
}

【问题讨论】:

    标签: c# button xna click


    【解决方案1】:

    你的逻辑有点混乱。一句话,你的代码是:如果按钮被点击,做更新,你检查它是否被点击。这样您就永远不会更新您的按钮。

    试试这个修改后的 Update 函数:

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        MouseState mouse = Mouse.GetState();
        //Debug.WriteLine(mouse);
    
        btn.Update(mouse);
        if (btn.Clicked == true)
        {
            Debug.WriteLine("Clicked");
            currentState = GameState.Playing;
        }
        base.Update(gameTime);
    }
    

    一句话中的这段代码的意思是:更新按钮,检查按钮是否被点击,然后,如果按钮被点击,则在调试中设置“已点击”并将gameState设置为正在播放。

    编辑 1:
    另外,我看不出颜色应该如何与点击相关联。 在按钮更新方法中,我担心代码的这种和平

            if (mouse.LeftButton == ButtonState.Pressed)
            {
                Clicked = true;
            }
            else if (_color.A < 255)
            {
                _color.A += 3;
                Clicked = false;
            }
    

    我宁愿这样写:

            if (mouse.LeftButton == ButtonState.Pressed)
            {
                Clicked = true;
            }
            else
            {
                if (_color.A < 255)
                    _color.A += 3;
                Clicked = false;
            }
    

    这样把这段代码放在一句话里就更简单了,而且这种简单性让它不管_color.A是什么值都可以工作,而且_color.A也会在你需要的时候更新。

    【讨论】:

    • 非常感谢您的观点。我试过你上面写的代码,效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 2013-01-17
    • 2020-03-26
    • 2012-03-14
    • 2019-04-26
    相关资源
    最近更新 更多