C# Monogame - 在悬停时更改色调颜色
C# Monogame - Change tint color on hover
我想要一个 class 来管理我游戏中的按钮。到目前为止,我有以下代码:
public class Button
{
private SpriteFont btnFont;
private string btnTxt;
private Vector2 btnPos;
private Color btnColour;
public Button(SpriteFont newFont, string newTxt, Vector2 newPos, Color newColour)
{
btnFont = newFont;
btnPos = newPos;
btnTxt = newTxt;
btnColour = newColour;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.DrawString(btnFont, btnTxt, btnPos, btnColour);
}
}
以及用法:
private Button btn_1;
private Color colour = new Color.CornflowerBlue;
private Rectangle buttonArea_1 = new Rectangle(24, 300, 192, 39);
// LoadContent() Method:
btn_1 = new Button(Game1.defaultFontBig, "Play Game", new Vector2(24, 300), colour);
// Update() Method:
MouseState mouseState = Mouse.GetState();
Point mousePosition = new Point(mouseState.X, mouseState.Y);
if (buttonArea_1.Contains(mousePosition))
{
colour = Color.Yellow;
if (mouseState.LeftButton == ButtonState.Pressed)
{
Game1.currentGameState = Game1.gameStates.loadingScreen;
}
}
else
{
colour = Color.CornflowerBlue;
}
// Draw Method():
btn_1.Draw(spriteBatch);
但出了点问题...当我测试游戏并将鼠标悬停在按钮上时,它不会将其颜色从矢车菊蓝色更改为黄色,但在我单击它时仍会更改游戏状态。我觉得这是我缺少的某种简单的初始化东西,所以我正在寻求您的帮助。任何代码 examples/approaches 解释都会很棒。
编辑:
通过这样的属性?
public Color Colour
{
public get { return colour; }
private set { colour = value; }
}
但是用法有变化吗?我仍然可以将颜色传递给构造函数并在 Update()
中更改它吗?
而不是:
private Color btnColour;
在按钮 class 中,执行此操作:
public Color btnColour { get; set; }
然后在使用代码中,而不是这个:
colour = Color.Yellow;
这样做:
btn_1.colour = Color.Yellow;
和:
btn_1.colour = Color.CornflowerBlue;
我想要一个 class 来管理我游戏中的按钮。到目前为止,我有以下代码:
public class Button
{
private SpriteFont btnFont;
private string btnTxt;
private Vector2 btnPos;
private Color btnColour;
public Button(SpriteFont newFont, string newTxt, Vector2 newPos, Color newColour)
{
btnFont = newFont;
btnPos = newPos;
btnTxt = newTxt;
btnColour = newColour;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.DrawString(btnFont, btnTxt, btnPos, btnColour);
}
}
以及用法:
private Button btn_1;
private Color colour = new Color.CornflowerBlue;
private Rectangle buttonArea_1 = new Rectangle(24, 300, 192, 39);
// LoadContent() Method:
btn_1 = new Button(Game1.defaultFontBig, "Play Game", new Vector2(24, 300), colour);
// Update() Method:
MouseState mouseState = Mouse.GetState();
Point mousePosition = new Point(mouseState.X, mouseState.Y);
if (buttonArea_1.Contains(mousePosition))
{
colour = Color.Yellow;
if (mouseState.LeftButton == ButtonState.Pressed)
{
Game1.currentGameState = Game1.gameStates.loadingScreen;
}
}
else
{
colour = Color.CornflowerBlue;
}
// Draw Method():
btn_1.Draw(spriteBatch);
但出了点问题...当我测试游戏并将鼠标悬停在按钮上时,它不会将其颜色从矢车菊蓝色更改为黄色,但在我单击它时仍会更改游戏状态。我觉得这是我缺少的某种简单的初始化东西,所以我正在寻求您的帮助。任何代码 examples/approaches 解释都会很棒。
编辑:
通过这样的属性?
public Color Colour
{
public get { return colour; }
private set { colour = value; }
}
但是用法有变化吗?我仍然可以将颜色传递给构造函数并在 Update()
中更改它吗?
而不是:
private Color btnColour;
在按钮 class 中,执行此操作:
public Color btnColour { get; set; }
然后在使用代码中,而不是这个:
colour = Color.Yellow;
这样做:
btn_1.colour = Color.Yellow;
和:
btn_1.colour = Color.CornflowerBlue;