2017-10-20 93 views
-2

我正在研究一個彈跳球程序,它已快完成了。我唯一的問題是,我不知道如何獲取用戶在NumericUpDown中選擇的數字並將其用作座標增量。這會使球更快,更慢,或者如果設置爲負數,則會使球從相反的方向開始。 這是我的代碼:如何獲取NumericUpDown值來更改PictureBox移動速度?

}

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值,讓用戶改變的速度球。 感謝您的關注。

回答

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(); 
    } 
} 

做同樣的事情全部打勾事件。

+0

我得到1個錯誤。在ball.top = x + 5;和ball.Left = y + 5;它說「不能隱式地將類型'decimal'轉換爲'int',存在顯式轉換」。我該如何解決這個問題? –

+0

已更新的答案。現在試試。 –

+0

剛剛意識到將x和y設置爲ball.Top和ball.Left與所有定時器搞砸了,因爲我使用了命令ball.Left + = 5;和ball.top + = 5;這與ball.Location = new Point(ball.Location.X +5,ball.Location.Y +5)相同; –