2013-03-07 69 views
0

我正在處理點擊事件,我不介意更新的閃電速度處理和繪製,這就是爲什麼我只是使用簡單的按鈕按下事件來處理點擊。但是和其他程序員一樣,我在使用這種方法時遇到了一個問題,我正在添加score + = 100等分數,正如您會猜到的那樣,分數的添加速度非常快,只需點擊一下即可比分增加了200-400。這是我如何做到的。在xna遊戲工作室中處理快速點擊

mouseStateCurrent = Mouse.GetState(); 
       mousePosition = new Point(mouseStateCurrent.X, mouseStateCurrent.Y); 
       if (drawPauseMenu == false) 
       { 
        if (pauseBtnRec.Contains(mousePosition)) 
        { 
         if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
         { 
          drawPauseMenu = true; 
          paused = true; 
         } 
        } 
        else if (binRec.Contains(mousePosition)) 
        { 
         if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
         { 
          playerScore += 100; 
          binSelected = 1; 
         } 

        } 
        else if (birdBathRec.Contains(mousePosition)) 
        { 
         if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
         { 
          playerScore += 100; 
          birdBathSelected = 1; 
         } 

        } 
        else if (bowlRec.Contains(mousePosition)) 
        { 
         if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
         { 
          playerScore += 100; 
          bowlSelected = 1; 
         } 

        } 
        else if (cansRec.Contains(mousePosition)) 
        { 
         if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
         { 
          playerScore += 100; 
          cansSelected = 1; 
         } 

        } 
        else if (paintsRec.Contains(mousePosition)) 
        { 
         if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
         { 
          playerScore += 100; 
          paintsSelected = 1; 
         } 

        } 
        else if (poolRec.Contains(mousePosition)) 
        { 
         if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
         { 
          playerScore += 100; 
          poolSelected = 1; 
         } 

        } 
        else if (pothRec.Contains(mousePosition)) 
        { 
         if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
         { 
          playerScore += 100; 
          potSelected = 1; 
         } 

        } 
        else if (tiresRec.Contains(mousePosition)) 
        { 
         if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
         { 
          playerScore += 100; 
          tiresSelected = 1; 

         } 

        } 
        else if (vasesRec.Contains(mousePosition)) 
        { 
         if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
         { 
          playerScore += 100; 
          vasesSelected = 1; 
         } 

        } 
        mouseStatePrevious = mouseStateCurrent; 
       } 

一直試圖與此代碼玩,並試圖做這種方式,

if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
{ 

    if (mouseStateCurrent.LeftButton == ButtonState.Released) 
    { 
     playerScore += 100; 
     vasesSelected = 1; 
    } 
} 

仍然沒有運氣與此有關。有任何想法嗎?謝謝!

回答

1

你快到了。你有一個mouseStatePrevious,你正確地設置它,但你永遠不會使用它。

相反的:

if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
{ // Why are you checking if the mouse is pressed AND released? 
    if (mouseStateCurrent.LeftButton == ButtonState.Released) 
    { 
     playerScore += 100; 
     vasesSelected = 1; 
    } 
} 

這樣做:

if (mouseStateCurrent.LeftButton == ButtonState.Pressed) 
{ 
    if (mouseStatePrevious.LeftButton == ButtonState.Released) 
    { 
     playerScore += 100; 
     vasesSelected = 1; 
    } 
} 
+0

其實我很久以前注意到這一點,我沒有使用我的mouseStatePrevious什麼。好吧,謝謝你的回答。一個簡單的問題。我可以注意到一些圖像並非真正準確放置。我在屏幕上使用矩形顯示它們的位置和大小,但有時它不會響應點擊。這是否意味着mousePosition並不真正擊中圖像的矩形?但是爲什麼從視覺上講,鼠標箭頭與圖像相交,但代碼沒有響應。對於長期的問題抱歉。謝謝! – ljpv14 2013-03-07 15:01:21

+0

根據[MSDN](http://msdn.microsoft.com/en-us/library/22t27w02.aspx):'必須對包含的矩形進行歸一化,以便此方法返回準確的結果。' – Nolonar 2013-03-07 15:05:17

+0

你是什麼意思歸一化? – ljpv14 2013-03-07 15:39:45