2015-04-01 12 views
0

即使定時器始終以100的間隔值開啓,我的代碼也不會使用tmrMoving(定時器),所以沒有理由我可以看到,但我希望這只是其中的一個,而且很簡單。代碼如下所示,我的程序不會調用要在C#中執行的代碼

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Globalization; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace games1 
{ 
public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     tmrMoving.Enabled = true; 
     Invalidate(); 
    } 

    private void tmrMoving_Tick(object sender, object value, Type targetType, 
     object parameter, CultureInfo culture, EventArgs e) 
    { 

     var cursPoint = new System.Drawing.Point(Cursor.Position.X, Cursor.Position.Y); 
     var playerPoint = new System.Drawing.Point(player.Location.X, player.Location.Y); 
     var diff = new Point(Cursor.Position.X - playerPoint.X, Cursor.Position.Y - playerPoint.Y); 
     var speed = Math.Sqrt(diff.X * diff.X + diff.Y * diff.Y); 
     if (speed > 10) 
     { 
      diff.X /= (int)(speed/10); 
      diff.Y /= (int)(speed/10); 
     } 
     player.Location = new System.Drawing.Point(player.Location.X + diff.X, player.Location.Y + diff.Y); 
    } 
    } 
} 

此鏈接到我的coding的圖像格式,如果你喜歡這種方式。

+1

什麼是您的問題? – MajkeloDev 2015-04-01 10:00:42

+0

你可以在代碼後面顯示相關行嗎? – 2015-04-01 10:00:53

+2

我從來沒有見過這麼多參數的計時器事件處理程序 - 這是一個'System.Windows.Forms.Timer'嗎? – 2015-04-01 10:03:24

回答

1

在我看來,你有兩個問題:

  1. 事件處理程序簽名不匹配對what I expected it to be。我希望它是:

    private void tmrMoving_Tick(object sender, EventArgs e) 
    
  2. 是不可能與連接到事件破碎事件處理程序進行編譯。因此,在我看來,它根本沒有分配。檢查的代碼做這個(可能在Designer.cs文件)的存在:

    this.tmrMoving.Tick += tmrMoving_Tick; 
    
+0

這可能是因爲它不在代碼中。我不明白爲什麼這還沒有被自動輸入,雖然...我從來沒有添加一個定時器到設計器文件的任何位置信息需要? – 2015-04-01 10:15:33

+1

只需雙擊設計器中的計時器即可。它會爲您創建一個事件處理程序並正確註冊它。 – 2015-04-01 10:17:34

+0

嘿計時器的過載會是什麼?沒有這些經驗? – 2015-04-01 10:19:33

相關問題