鼠标控制,我希望有一个pictureBox以一定的速率跟随鼠标光标

时间:2015-03-30 09:20:20

标签: c# vb.net visual-studio-2010 mouseevent mouselistener

首次访问光标的移动属性时,这里有点丢失。 我希望在启用fst_click(bool)后,pictureBox(播放器)向鼠标光标缓慢移动。我尝试了很多方法但没有成功。我已经尝试了一些案例,但我的返回方法无法解决这个问题吗?

我虽然可能是某种类型的鼠标监听器,但要更新鼠标的位置并根据计时器的速度移动到该位置,但路径绝对不是我的强项。

private int _x;
    private int _y;
    private bool MRight = false;
    private bool MLeft = false;
    private bool MUp = false;
    private bool MDown = false;
    bool fst_click = false;
    private Position _objPosition;

    public Form1()
    {
        InitializeComponent();
    }


    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        clicking = true;
    }
    public void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Space)
        { clicking = true; }
    }

我删除了不正确的case语句并添加了不同的bool。这个陈述确实在4种情况W,A,S,D之间切换,但玩家位置无法顺利更新? 我还添加了一个计时器来尝试插入一个可以创建平滑移动的布尔值,但我又无法与玩家相关联。位置?

public void tmrMoving_Tick(object sender, EventArgs e)
    {
        if (clicking == true)
        {
            player.Location = Cursor.Position;
            clicking = false;
        }
     }

    private void Form1_MouseMoveHandler(object sender, MouseEventArgs e)
    {
        //Point mousePoint = new Point(player.Location.X, player.Location.Y);
    }

更新 我已经添加了下面的新编码,如果有任何关于如何修复它的想法,因为我没有得到任何功能。光标需要以特定速度向点击位置移动,注释掉的代码尝试失败,我希望将其替换。

namespace games1
 {
  public partial class Form1 : Form
   {
     public Form1()
     {
        InitializeComponent();
     }
    private void Form1_MouseClick(object sender, MouseEventArgs e)
     {
            tmrMoving.Enabled = true;
            Invalidate();
     }
    private void tmrMoving_Tick(object sender, EventArgs e)
    {
            var xdiff = Cursor.Position.X - player.Location.X;
            var ydiff = Cursor.Position.Y - player.Location.Y; 
            var diff = Math.Sqrt(xdiff + ydiff);

           //    diff = ydiff / 10;
           //    diff = xdiff / 10;

           // xdiff = player.Location.X;
           // ydiff = player.Location.Y;
    }
  }
}

0 个答案:

没有答案