2011-11-27 56 views
0

我做了一個太空入侵者遊戲。起初,當我移動鼠標時,遊戲運行緩慢,因爲我有一個鼠標移動事件,所以有人告訴我,這是我的無效方法。我相應地改變了它,遊戲速度更好。但它現在不能清除舊圖像。它留下了一些圖像。鼠標移動離開第th圖像路徑

請幫忙!

MOUSE_MOVE事件

private void Form1_MouseMove(object sender, MouseEventArgs e) 
    { 
     Cursor.Dispose(); 
     objsp.gsPos = new Point(MousePosition.X/2 - 10, MousePosition.Y/2 - 15); 
     UpdatePosition(objsp.gsPos.X, objsp.gsPos.Y, objsp.gsImage); 
    } 

正被調用

private void UpdatePosition(int dx, int dy, Image img) 
    {    
     Point newPos = new Point(objsp.gsPos.X + dx, objsp.gsPos.Y + dy); 

     //dont go out of window boundary 
     newPos.X = Math.Max(0, Math.Min(ClientSize.Width - img.Width, newPos.X)); 
     newPos.Y = Math.Max(0, Math.Min(ClientSize.Height - img.Height, newPos.Y)); 

     if (newPos != objsp.gsPos) 
     { 
      objsp.gsPos = newPos; 
      Rectangle rc = new Rectangle(objsp.gsPos, img.Size); 
      Invalidate(rc);    
     } 
    } 

窗體加載輸出enter image description here

一旦UpdatePosition方法鼠標移動輸出

enter image description here

+0

我敢打賭,你的油漆處理程序未重繪它被要求重繪 –

+0

**私人無效Form1_Paint(對象發件人,PaintEventArgs的E){ e.Graphics.DrawImage(objsp.gsImage,objsp.gsPos.X, objsp.gsPos.Y,objsp.gsImage.Width,objsp.gsImage.Height); } ** –

+0

看起來您並未更新整個區域 –

回答

1

UpdatePosition當前和以前的鼠標位置無效。

+0

** Private void Form1_Paint(object sender,PaintEventArgs e) e.Graphics.DrawImage(objsp.gsImage,objsp.gsPos.X,objsp。 gsPos.Y,objsp.gsImage.Width,objsp.gsImage.Height); } **我注意到,當至少有10個入侵者死亡時,遊戲正常運行......現在感到困惑。 –

+0

最有可能是因爲減少意味着更快的處理。在精靈移動很遠之前,每個油漆都會更快。 MagnatLU的解決方案將解決這兩種情況。 –