2013-05-13 61 views
1

抵消我這裏得到鼠標的位置,並繪製它的一些簡單的代碼:XNA鼠標位置在很​​大程度上是由實際位置

class Pointer 
{ 
    MouseState currPointerState; 
    Vector2 currPointerPos; 


    public Pointer() 
    { 
    } 

    public void Update() 
    { 
     currPointerState = Mouse.GetState(); 
     currPointerPos.X = currPointerState.X; 
     currPointerPos.Y = currPointerState.Y; 
    } 

    public void Draw(SpriteBatch spriteBatch, Texture2D pointerTexture) 
    { 
     spriteBatch.Draw(pointerTexture, currPointerPos, Color.White); 
    } 

} 

然後在我的遊戲主文件:

 protected override void Update(GameTime gameTime) 
    { 
     pointer.Update(); 
     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     spriteBatch.Begin(); 

     pointer.Draw(spriteBatch, pointerTexture); 
     menuManager.Draw(spriteBatch, menu_bar); 

     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 

遊戲現在在一個窗口中運行,但鼠標右邊約500像素,遠低於實際鼠標位置100像素。

發生這種情況後,我加入以下代碼:(菜單拉絲級)

enum MenuState 
{ 
    mainMenu, 
    playing, 
    options 
}; 

class MenuManager : Game1 
{ 
    MenuState menuState = MenuState.mainMenu; 
    Vector2 button1 = Vector2.Zero; 


    public void Draw(SpriteBatch spriteBatch, Texture2D menuBar) 
    { 
     switch (menuState) 
     { 
      case MenuState.mainMenu: 
       spriteBatch.Draw(menuBar, button1, Color.White); 
        break; 



     } 
    } 
} 

}

任何想法,爲什麼鼠標的位置可能已經改變了嗎?

回答

4

所以我想通了。

如果有人遇到問題,解決辦法是刪除:Game1參考作爲低音類。遊戲課程不止一次被創建,因此窗口座標被堆疊。