-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值,讓用戶改變的速度球。 感謝您的關注。
我得到1個錯誤。在ball.top = x + 5;和ball.Left = y + 5;它說「不能隱式地將類型'decimal'轉換爲'int',存在顯式轉換」。我該如何解決這個問題? –
已更新的答案。現在試試。 –
剛剛意識到將x和y設置爲ball.Top和ball.Left與所有定時器搞砸了,因爲我使用了命令ball.Left + = 5;和ball.top + = 5;這與ball.Location = new Point(ball.Location.X +5,ball.Location.Y +5)相同; –