如何获取NumericUpDown值以更改PictureBox移动速度?

时间:2017-10-20 19:32:41

标签: c# picturebox numericupdown

我正在制作一个弹跳球计划,它几乎已经完成了。我唯一的问题是我不知道如何获取用户在NumericUpDown中选择的数字并将其用作坐标增量。这使得球变得更快,更慢,或者如果设置为负数,则使其从相反方向开始。 这是我的代码:

}

    private void NumericUpDownVx_ValueChanged(object sender, EventArgs e)
    {

    }

    private void NumericUpDownVy_ValueChanged(object sender, EventArgs e)
    {

    }

    private void ButtonStart_Click(object sender, EventArgs e)
    {


        if (buttonStart.Text == "Start")

        {
            numericUpDownVx.Enabled = false;
            numericUpDownVy.Enabled = false;

            buttonStart.Text = "Stop";

            Timer1.Start();

        } 


        else if (buttonStart.Text == "Stop")
        {
            numericUpDownVx.Enabled = true;
            numericUpDownVy.Enabled = true;
            buttonStart.Text = "Start";
            Timer1.Stop();
            Timer2.Stop();
            Timer3.Stop();
            Timer4.Stop();
        }
    }

    private void Timer1_Tick(object sender, EventArgs e)
    {
        ball.Top += 5;
        ball.Left += 5;

        if (ball.Left + ball.Width > Mesa.Width)
        {
            Timer1.Stop();
            Timer2.Start();
        }

        if (ball.Top + ball.Height > Mesa.Height)
        {
            Timer1.Stop();
            Timer3.Start();   
        }
    }

    private void Timer2_Tick(object sender, EventArgs e)
    {
        ball.Top += 5;
        ball.Left -= 5;

        if (ball.Top + ball.Height > Mesa.Height)
        {
            Timer2.Stop();
            Timer4.Start();
        }

        if (ball.Left < 0)
        {
            Timer2.Stop();
            Timer1.Start();
        }
    }

    private void Timer3_Tick(object sender, EventArgs e)
    {
        ball.Top -= 5;
        ball.Left += 5;

        if (ball.Left + ball.Width > Mesa.Width)
        {
            Timer3.Stop();
            Timer4.Start();
        }

        if (ball.Top < 0)
        {
            Timer3.Stop();
            Timer1.Start();
        }
    }

    private void Timer4_Tick(object sender, EventArgs e)
    {
        ball.Top -= 5;
        ball.Left -= 5;

        if (ball.Left < 0)
        {
            Timer4.Stop();
            Timer3.Start();
        }

        if (ball.Top < 0)
        {
            Timer4.Stop();
            Timer2.Start();
        }
    }

NumericUpDown值从-5变为5,我需要它来替换ball.Top和ball.Left值让用户改变球的速度。 感谢您的关注。

1 个答案:

答案 0 :(得分:0)

您可以声明全局变量以保存x和y值。在表单加载事件

中将它们初始化为x =ball.Topy = ball.Left
decimal x=0;
decimal y =0;

private void NumericUpDownVx_ValueChanged(object sender, EventArgs e)
{
     x = NumericUpDownVx.Value;
}

private void NumericUpDownVy_ValueChanged(object sender, EventArgs e)
{
      y = NumericUpDownVy.Value;
}

private void ButtonStart_Click(object sender, EventArgs e)
{


    if (buttonStart.Text == "Start")

    {
        numericUpDownVx.Enabled = false;
        numericUpDownVy.Enabled = false;

        buttonStart.Text = "Stop";

        Timer1.Start();

    } 


    else if (buttonStart.Text == "Stop")
    {
        numericUpDownVx.Enabled = true;
        numericUpDownVy.Enabled = true;
        buttonStart.Text = "Start";
        Timer1.Stop();
        Timer2.Stop();
        Timer3.Stop();
        Timer4.Stop();
    }
}

private void Timer1_Tick(object sender, EventArgs e)
{
    ball.Top = Convert.ToInt32(x + 5);
    ball.Left = Convert.ToInt32(y + 5);

    if (ball.Left + ball.Width > Mesa.Width)
    {
        Timer1.Stop();
        Timer2.Start();
    }

    if (ball.Top + ball.Height > Mesa.Height)
    {
        Timer1.Stop();
        Timer3.Start();   
    }
}

对所有嘀嗒事件做同样的事情。