我的程序不会调用要在C#中执行的代码

时间:2015-04-01 09:57:39

标签: c# winforms

我的代码根本不会使用tmrMoving(定时器),即使定时器持续打开,间隔值为100,所以没有理由我可以看到,但我希望它和&#m} #39;这只是其中的一天而且很简单。代码如下,

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 个答案:

答案 0 :(得分:1)

在我看来,你有两个问题:

  1. 您的事件处理程序签名与what I expected it to be不匹配。我原以为:

    private void tmrMoving_Tick(object sender, EventArgs e)
    
  2. 无法使用附加到事件的已损坏事件处理程序进行编译。因此在我看来它根本没有分配。检查代码是否存在(可能在Designer.cs文件中):

    this.tmrMoving.Tick += tmrMoving_Tick;