我有一个名为CraftButton
的按钮类,它有一个名为isClicked
的变量,而在另一个类中我创建了Button
类的2个实例,如下所示:
Button b1, b2;
我还有一个名为FunctionizeButtons
的方法,用于检查我是否点击了按钮:
if (bP1.isClicked)
{
Game1.Instance.examineString = "button 1";
}
if (b2.isClicked)
{
Game1.Instance.examineString = "button2";
}
但是当我尝试按钮时,只有b1响应。
isClicked
变量不适用于b2,但在b1上,为什么会这样?
我的按钮类:
public class CraftButton
{
public Texture2D _texNormal, _texPushed, _texHighlighted, currentTex;
public Vector2 position;
Rectangle rectangle;
public bool isPushed = false;
public bool isClicked = false;
public CraftButton(ContentManager Content)
{
_texNormal = Content.Load<Texture2D>("UI/button");
_texPushed = Content.Load<Texture2D>("UI/buttonPushed");
_texHighlighted = Content.Load<Texture2D>("UI/buttonHighlighted");
rectangle = new Rectangle((int)position.X, (int)position.Y, _texNormal.Width, _texNormal.Height);
currentTex = _texNormal;
}
public void Update(GameTime gameTime)
{
rectangle = new Rectangle((int)position.X, (int)position.Y, _texNormal.Width, _texNormal.Height);
if (rectangle.Contains(Game1.Instance.mPoint))
currentTex = _texHighlighted;
else
currentTex = _texNormal;
if (Game1.Instance.mState.LeftButton == ButtonState.Pressed && rectangle.Contains(Game1.Instance.mPoint))
{
currentTex = _texPushed;
isPushed = true;
}
else
{
isPushed = false;
}
if (Game1.Instance.mState.LeftButton == ButtonState.Pressed && Game1.Instance.oldMstate.LeftButton == ButtonState.Released && rectangle.Contains(Game1.Instance.mPoint))
{
isClicked = true;
}
else
isClicked = false;
Game1.Instance.oldMstate = Game1.Instance.mState;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(currentTex, position, Color.White);
}
}
}
我的意思是它不起作用,b2
&#39; isClicked
变量一直是假的,它不会被点击作为b1
确实如此。
我真的很困惑,希望得到一些帮助,谢谢!