我是 XNA 的新手,我遇到了一个问题。我有一个按钮类,用于游戏开始屏幕上的按钮。我想这样做,以便当鼠标单击按钮时,布尔值 isClicked 设置为 true,然后您可以对按钮执行任何操作。但是,当我编译游戏时,我似乎不能直接点击按钮的矩形(或它应该在的位置),而是必须点击它的下方或上方,每次运行游戏时都会改变。
我有这个按钮类的代码:
class cButton
{
Texture2D texture;
public Vector2 position;
public Rectangle rectangle;
public Rectangle mouseRectangle;
public Vector2 mousePosition;
public cButton(Texture2D newTexture, Vector2 newPosition)
{
texture = newTexture;
position = newPosition;
rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
}
bool down;
public bool isClicked;
public void Update(MouseState mouse, GameTime gameTime)
{
mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
mousePosition = new Vector2(mouse.X, mouse.Y);
if (mouseRectangle.Intersects(rectangle))
{
if (mouse.LeftButton == ButtonState.Pressed)// if mouse is on button
{
isClicked = true;
}
else
{
isClicked = false;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, color);
}
}
}
游戏 1 类中用于绘制该按钮的代码:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
switch (CurrentGameState)
{
case GameState.MainMenu:
spriteBatch.Begin();
spriteBatch.Draw(Content.Load<Texture2D>("Backgrounds/title"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
btnPlay.Draw(spriteBatch);
spriteBatch.End();
break;
}
base.Draw(gameTime);
}
我在想这可能与我为它设置的屏幕分辨率有关,代码在这里:
//Screen Adjustments
public int screenWidth = 1280, screenHeight = 720;
graphics.PreferredBackBufferWidth = screenWidth;
graphics.PreferredBackBufferHeight = screenHeight;
请帮忙,我不知道我做错了什么。