Я не мог разобраться в проблеме, поэтому после отладки я наконец решил написать ниже.
Почему место прыгает вокруг ?! 147 86 до 294 212 затем обратно каждый обратный звонок?
pic.MouseMove += my_MouseMove;
my_MouseMove(object sender, MouseEventArgs e)
Console.WriteLine("{0} {1} {2} {3}", e.X, e.Y, e.Location.X, e.Location.Y);
147 86 147 86
294 212 294 212
147 86 147 86
294 212 294 212
147 86 147 86
294 212 294 212
//This code expects e.X,Y to return the X,Y Relative to the parent control.
//It seems to be relative to mousedown which breaks this.
Point heroClick = new Point();
private void hero_MouseDown(object sender, MouseEventArgs e)
{
heroClick.X = e.X;
heroClick.Y = e.Y;
}
private void hero_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != System.Windows.Forms.MouseButtons.Left)
return;
var xDif = e.X - heroClick.X;
var yDif = e.Y - heroClick.Y;
heroClick.X += xDif;
heroClick.Y += yDif;
lvl.playerX += xDif;
lvl.playerY += yDif;
PictureBox pic = ((PictureBox)sender);
pic.Left += xDif;
pic.Top+= yDif;
//Console.WriteLine("{0} {1} {2} {3}", e.X, e.Y, e.Location.X, e.Location.Y, sender);
textBox2.Text = string.Format("{0} {1} {2} {3}", e.X, e.Y, e.Location.X, e.Location.Y, sender);
textBox2.Update();
}