【问题标题】:am trying to make c# event puzzle game on for an assignment我正在尝试为一项任务制作 c# 事件益智游戏
【发布时间】:2014-07-09 21:39:24
【问题描述】:

我让图片框移动了。但是当我点击图片框时,它会跳遍整个地方。谁能帮我解决这个问题?

namespace Move_Shapes
{
    public partial class Form1 : Form
    {
        int X = 0;
        int Y = 0;
        int mX = 0;
        int mY = 0;
        bool Move = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void pic_TL_Click(object sender, EventArgs e)
        {
            //X = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
            //Y = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;

            //label1.Text = X.ToString();
            //label2.Text = Y.ToString();

            //X = MousePosition.X - this.Location.X - 8;
            //Y = MousePosition.Y - this.Location.Y - 30;

            //label3.Text = X.ToString();
            //label4.Text = Y.ToString();
        }

        private void pic_TL_MouseDown(object sender, MouseEventArgs e)
        {
            X = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
            Y = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;

            label1.Text = X.ToString();
            label2.Text = Y.ToString();
            Move = true;
        }

        private void pic_TL_MouseMove(object sender, MouseEventArgs e)
        {
            mX = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
            mY = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;

            if (Move)
            {
                pic_TL.Location = new Point(mX - X, mY - Y);
            }

        }

        private void pic_TL_MouseUp(object sender, MouseEventArgs e)
        {
            Move = false;
        }
    }
}

【问题讨论】:

  • 请修改您的标题以解释您的问题而不是您的一般情况。
  • 对此很陌生,但基本上当我点击鼠标时会发生什么,图片框会前后跳动,不知道为什么
  • 这些神奇的常数是什么:8、30?
  • 表格左侧的距离与表格顶部的高度
  • 跳跃,因为公式错误。基本上,记住MouseDown 事件中的鼠标位置(顺便说一句,从e 获取,而不是从MousePosition 获取)和MouseMove 事件更改位置:pic_TL.X += oldMouseX - e.XY 相同。

标签: c# event-driven-design


【解决方案1】:

在 MoseMove 上,您将位置设置为当前鼠标位置和初始鼠标位置的差值。

pic_TL.Location = new Point(mX - X, mY - Y);

如果鼠标移过一个像素,图片会移动到左上角。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多