2013-10-29 37 views
0

在我目前正在處理的遊戲中,我有一個播放按鈕,其中有一個Vector2,一個Rectangle和一個Texture2D。但不知何故,當我運行遊戲時,播放按鈕是不可見的,但它仍然對鼠標碰撞/檢測作出反應。
這是我的代碼:矩形使Texture2D隱形

Texture2D buttonPlay; 
    Rectangle buttonPlayRect; 
    Vector2 buttonPlayPos; 

    Point mousePosition; 

    int resolutionX = ScreenManager.GraphicsDeviceMgr.PreferredBackBufferWidth; 
    int resolutionY = ScreenManager.GraphicsDeviceMgr.PreferredBackBufferHeight; 

    public void Initialize() 
    { 
     buttonPlayPos = new Vector2(resolutionX/2 - 64, resolutionY/2 - 64); 
    } 

    public override void LoadAssets() 
    { 
     buttonOptionsPos = new Vector2(resolutionX/2 - 64, resolutionY/2); 

     backgroundTile = ScreenManager.ContentMgr.Load<Texture2D>("Menu/background"); 
     buttonOptions = ScreenManager.ContentMgr.Load<Texture2D>("Menu/optionsButton"); 
     buttonPlay = ScreenManager.ContentMgr.Load<Texture2D>("Menu/playButton"); 

     buttonPlayRect = new Rectangle((int)buttonPlayPos.X, (int)buttonPlayPos.Y, buttonPlay.Width, buttonPlay.Height); 

     base.LoadAssets(); 
    } 

    public override void Update(Microsoft.Xna.Framework.GameTime gameTime) 
    { 
     MouseState mState = Mouse.GetState(); 
     mousePosition = new Point(mState.X, mState.Y); 

     base.Update(gameTime); 
    } 

    public override void Draw(Microsoft.Xna.Framework.GameTime gameTime) 
    { 
     for (int x = 0; x < 10; x++) 
     { 
      for (int y = 0; y < 10; y++) 
      { 
       ScreenManager.Sprites.Draw(backgroundTile, new Vector2(tileWidth * x, tileHeight * y), Color.White); 
      } 
     } 

     ScreenManager.Sprites.Draw(buttonPlay, buttonPlayPos, buttonPlayRect, Color.White); 
     ScreenManager.Sprites.Draw(buttonOptions, buttonOptionsPos, Color.White); 

     if (buttonPlayRect.Contains(mousePosition)) 
     { 

     } 

     base.Draw(gameTime); 
    } 
} 

我有這個問題與藏漢其他項目的同時,是什麼讓Texture2D不會出現?提前致謝!

+0

'buttonOptions'顯示嗎? – Robb

+0

是的,它確實但我不用'buttonOptionsRect'繪製它。 – QuackTheDuck

回答

0

當您繪製某些東西時,我建議您只使用或SpriteBatch.DrawdestinationRectangle,有時它會出現意外情況。
更重要的是,如果您將它們作爲參數傳遞,沒有理由設置位置並使用它來聲明矩形。

作爲一個建議,你應該檢查buttonPlayRect.Contains(mousePosition)你的Update方法,Draw應該只管理你的遊戲繪圖,而不是邏輯。

UPDATE

如果您需要更改取決於當前的分辨率的按鈕的現在的位置,就可以吸引他們宣佈在每一個Draw週期的位置

new Vector2(resolutionX/2 - 64, resolutionY/2 - 64); 

,或者你可以添加一個事件處理程序以這種方式:

在您的Initialize方法中添加此項:

Game.Window.ClientSizeChanged += Window_ClientSizeChanged; 

那麼像這樣的東西創建Window_ClientSizeChanged方法:

private void Window_ClientSizeChanged(object sender, EventArgs e) 
{ 
    buttonPlayPos = new Vector2(resolutionX/2 - 64, resolutionY/2 - 64); 
    buttonPlayRect = new Rectangle((int)buttonPlayPos.X, (int)buttonPlayPos.Y, buttonPlay.Width, buttonPlay.Height); 
} 

當然resolutionXresolutionY必須更新的值。

+0

謝謝!但是現在,我的決議X和決議Y根本不改變按鈕的位置,我應該以另一種方式聲明它,因爲我希望它們在屏幕中間,無論分辨率如何:/ – QuackTheDuck

+0

而且它們是以前的代碼?您可以將一個事件處理程序添加到'Game.Window.ClientSizeChanged'並更改按鈕的位置。 – pinckerman

+0

對不起,但我真的不明白,因爲我在XNA的初學者狀態D: – QuackTheDuck