我在XNA方面相當新,而且遇到了問題。 我有一個按鈕類,我正在使用遊戲的開始屏幕上的按鈕。我想做到這一點,當鼠標點擊按鈕時,一個布爾isClicked設置爲true,然後你可以做任何你想要的按鈕。然而,當我編譯遊戲時,似乎我不能直接點擊按鈕的矩形(或者它應該在的位置),但是我必須點擊它的下方或上方,每次運行遊戲時都會發生變化。XNA矩形相交問題
我有一個按鈕類的代碼:在比賽還有1類
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);
}
}
}
而這種代碼繪製按鈕:
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;
請幫助,我不知道我做錯了什麼。
屏幕分辨率更改不會影響XNA中鼠標的輸入,觸摸輸入會受到設置後置緩衝器的影響。 – MrME 2013-03-25 19:45:19