【问题标题】:Making a button Xna / MonoGame制作按钮 Xna / MonoGame
【发布时间】:2015-09-07 03:15:50
【问题描述】:

我想知道在 MonoGame 中制作按钮的最佳方法是什么。我试图寻找答案,但似乎没有答案。使用 WinForm 按钮是不好的做法吗?如果有,有哪些可行的替代方案?

【问题讨论】:

  • 这是不好的做法吗?这取决于你想要做什么。这是在游戏中使用的按钮(例如标题屏幕上的“播放”按钮)还是您想制作某种关卡编辑器?
  • @craftworkgames 会在游戏中使用

标签: c# user-interface xna xna-4.0 monogame


【解决方案1】:

我一直喜欢制作自定义按钮类,这为我创建创意按钮提供了很大的灵活性。

该类创建一个带有纹理和位置 x 和位置 y 以及唯一名称的按钮,在我完成之后,我将检查我的鼠标位置并查看它是否在按钮内,如果它在里面按钮然后它可以单击按钮,它将按名称搜索按钮并执行给定的命令:)

这是我的按钮类的示例:(不是最好的方法,但它对我来说非常有效)

public class Button : GameObject
    {
        int buttonX, buttonY;

        public int ButtonX
        {
            get
            {
                return buttonX;
            }
        }

        public int ButtonY
        {
            get
            {
                return buttonY;
            }
        }

        public Button(string name, Texture2D texture, int buttonX, int buttonY)
        {
            this.Name = name;
            this.Texture = texture;
            this.buttonX = buttonX;
            this.buttonY = buttonY;
        }

        /**
         * @return true: If a player enters the button with mouse
         */
        public bool enterButton()
        {
            if (MouseInput.getMouseX() < buttonX + Texture.Width &&
                    MouseInput.getMouseX() > buttonX &&
                    MouseInput.getMouseY() < buttonY + Texture.Height &&
                    MouseInput.getMouseY() > buttonY)
            {
                return true;
            }
            return false;
        }

        public void Update(GameTime gameTime)
        {
            if (enterButton() && MouseInput.LastMouseState.LeftButton == ButtonState.Released && MouseInput.MouseState.LeftButton == ButtonState.Pressed)
            {
                switch (Name)
                {
                    case "buy_normal_fish": //the name of the button
                        if (Player.Gold >= 10)
                        {
                            ScreenManager.addFriendly("normal_fish", new Vector2(100, 100), 100, -3, 10, 100);
                            Player.Gold -= 10;
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        public void Draw()
        {
            Screens.ScreenManager.Sprites.Draw(Texture, new Rectangle((int)ButtonX, (int)ButtonY, Texture.Width, Texture.Height), Color.White);   
        } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-25
    • 2021-10-06
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 2016-01-13
    相关资源
    最近更新 更多