当代码从keydown事件转移到函数时,c#变量重置 - 有人可以解释原因吗?

时间:2018-01-24 09:44:20

标签: c# visual-studio-2010

我在VS Express 2010中使用C#windows窗体应用程序。我正在玩一些代码用于学习目的。我有一个表单对象,我希望通过按键继续向一个方向移动 - 原始代码可以正常工作。

在通过将“移动”代码移动到函数中来尝试整理它的过程中,代码不再像以前那样工作。而不是我的对象从所选方向的当前位置移动,它现在每次按下一个键时重置其位置。我想知道为什么会这样,因为我所做的改变绝对是微不足道的。请参阅代码:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{          
    if (e.KeyCode == Keys.Down)
    {
        direction = 4;
    }
    if (e.KeyCode == Keys.Up)
    {
        direction = 2;
    }
    if (e.KeyCode == Keys.Right)
    {
        direction = 3;
    }
    if (e.KeyCode == Keys.Left)
    {
        direction = 1;
    }

    while (direction != 0)
    {

        Application.DoEvents();
        if (direction == 1)
        {
            X = X - 1;
        }
        else if (direction == 2)//up
        {
            Y = Y - 1;
        }
        else if (direction == 3)
        {
            X = X + 1;
        }
        else if (direction == 4)//down
        {
            Y = Y + 1;
        }

        Thread.Sleep(100);
        label1.Location = new Point(X, Y);
    }
}

当我将while循环移动到移动函数时,每次按键时变量X和Y都会重置为0。代码如下:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{          
  if (e.KeyCode == Keys.Down)
  {
      direction = 4;
  }

  if (e.KeyCode == Keys.Up)
  {
      direction = 2;
  }

  if (e.KeyCode == Keys.Right)
  {
      direction = 3;
  }

  if (e.KeyCode == Keys.Left)
  {
      direction = 1;
  }

  movement(X, Y, direction);
}

我觉得我在这里遗漏了一些明显的东西,但我不明白为什么它表现得与众不同。感谢您的帮助:))

编辑1:移动功能的代码:

    movement(int X, int Y, int direction)
    {
        while (direction != 0)
        {
            Application.DoEvents();
            if (direction == 1)
            {
                X = X - 1;
            }
            else if (direction == 2)//up
            {
                Y = Y - 1;
            }
            else if (direction == 3)
            {
                X = X + 1;
            }
            else if (direction == 4)//down
            {
                Y = Y + 1;
            }
            Thread.Sleep(100);
            label1.Location = new Point(X, Y);

        }            
    }

2 个答案:

答案 0 :(得分:1)

I guess your movement method changes X and Y which were supplied as parameters. However, the parameters X and Y are passed 'by value', not 'by reference'.

If you want to make this work, you either have to use class variables, and remove the parameters in the method call, or use ref, which I will demonstrate:

movement(ref X, ref Y, direction);

And:

private void movement(ref int X, ref int Y, int direction)
{ }

答案 1 :(得分:0)

private void Form1_KeyDown(object sender, KeyEventArgs e)
{          
    if      (e.KeyCode == Keys.Down)  direction = 4;
    else if (e.KeyCode == Keys.Up)    direction = 2;
    else if (e.KeyCode == Keys.Right) direction = 3;
    else if (e.KeyCode == Keys.Left)  direction = 1;

    while (direction != 0)
    {
        Application.DoEvents();

        if      (direction == 1) X--;
        else if (direction == 2) Y--;
        else if (direction == 3) X++;
        else if (direction == 4) Y++;

        Thread.Sleep(100);
        label1.Location = new Point(X, Y);
    }
}

可以改写:

private void SetDirection(KeyEventArgs e)
{
    if      (e.KeyCode == Keys.Down)  direction = 4;
    else if (e.KeyCode == Keys.Up)    direction = 2;
    else if (e.KeyCode == Keys.Right) direction = 3;
    else if (e.KeyCode == Keys.Left)  direction = 1;
}

private void ApplyMovement()
{
    while (direction != 0)
    {
        Application.DoEvents();

        if      (direction == 1) X--;
        else if (direction == 2) Y--;
        else if (direction == 3) X++;
        else if (direction == 4) Y++;

        Thread.Sleep(100);
        label1.Location = new Point(X, Y);
    }
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{          
    SetDirection(e);

    ApplyMovement();
}

您必须了解变量XYdirection是类成员,因此可以从类中的任何位置访问它们,并且当您调用{{1}时然后movement(X, Y, direction)使用这些变量的副本,因此当您在movement方法中执行X = X + 1时,实际上并未更改movement您的类的值,但仅限于它的副本。 这意味着每次按键时变量X和Y(即类成员)都不会重置为0。实际上 它们永远不会被设置为

请注意,您也可以使用X语句代替所有switch个语句。

if