2013-06-01 79 views
1

基本上,當我嘗試創建OpenFileDialog時,通過單擊「遊戲」中的特定區域,我的mouseState即使已釋放,仍保持「按下」狀態。OpenFileDialog破壞mouseState

OpenFileDialog dialog; 
protected override void Update(GameTime gameTime) 
    { 

     if (selectSong.isClicked) 
     { 
      DialogResult result = dialog.ShowDialog(); 
      if (result == DialogResult.OK) 
      { 
       //code; 

      } 
      else 
      { 
       selectSong.isClicked = false; 
       // Disabling mouse event - because isClicked is always true after using showDialog() 
      } 
     } 
     UpdateMouse(); 
} 
protected void UpdateMouse() 
    { 
     MouseState current_mouse = Mouse.GetState(); 
     Console.Clear(); 
     int mouseX = current_mouse.X; 
     int mouseY = current_mouse.Y; 
     selectSong.checkCollision(mouseX, mouseY, current_mouse.LeftButton); 
     exit.checkCollision(mouseX, mouseY, current_mouse.LeftButton); 
     Console.WriteLine("mouse X:" + mouseX + " mouse Y:" + mouseY); 
     Console.WriteLine("Left button down:" + current_mouse.LeftButton.ToString()); 
     // Left button down - after creating Dialog.ShowDialog() is always pressed, even after releasing mouse, and hitting cancel in Dialog 
    } 

這裏描述問題http://db.tt/NVnEUgLI

+0

是'UpdateMouse('是文件對話框打開後調用? (添加一個斷點) – Cyral

+0

當然,在.showDialog()上添加斷點後,並在我的updatemouse()函數中添加斷點 - 它會不斷更新,每次都是 - 每次左鍵都被按下 - 即使它被釋放了。 –

+0

嘗試使用Mouse.GetState()。LeftButton.ToString()來測試您是否正確更新current_mouse? –

回答

0

你的問題是截圖,你按在gamewindow鼠標,你釋放它在打開文件對話框。所以遊戲窗口不再更新鼠標。我想你可以在鼠標釋放後打開filedialog時解決這個問題。 對於您需要記住的最後一幀的值)

MouseButton last_frame = MouseButton.Released; 

protected void UpdateMouse() 
{ 
    MouseState current_mouse = Mouse.GetState(); 
    Console.Clear(); 
    int mouseX = current_mouse.X; 
    int mouseY = current_mouse.Y; 
    selectSong.checkCollision(mouseX, mouseY, current_mouse.LeftButton, last_frame); 
    exit.checkCollision(mouseX, mouseY, current_mouse.LeftButton, last_frame); 
    last_frame = current_mouse.LeftButton; 
    Console.WriteLine("mouse X:" + mouseX + " mouse Y:" + mouseY); 
    Console.WriteLine("Left button down:" + current_mouse.LeftButton.ToString()); 
    // Left button down - after creating Dialog.ShowDialog() is always pressed, even after releasing mouse, and hitting cancel in Dialog 
} 

obj.checkCollision你需要把

if(state_now == ButtonState.Released && state_last == ButtonState.Pressed &&...) 

,而不是

if(state_now == ButtonState.Pressed &&...)