2017-10-20 1 views
-2

나는 튀는 공 프로그램에 종사하고있어 거의 끝났습니다. 내 유일한 문제는 NumericUpDown에서 사용자가 선택한 숫자를 가져 와서 좌표 증분으로 사용하는 방법을 모르겠다는 것입니다. 이것은 볼을 더 빠르게, 느리게 만들거나, 음수로 설정하면 반대 방향으로 시작하게 만듭니다. 이 내 코드입니다 :PictureBox 이동 속도를 변경하려면 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 값을 대체하기 위해 필요 공. 관심을 가져 주셔서 감사합니다.

답변

0

x 및 y 값을 저장하도록 전역 변수를 선언 할 수 있습니다. 모든 틱 이벤트 같은 일을 할 폼로드 이벤트

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

x =ball.Topy = ball.Left로 초기화합니다.

+0

오류가 1 개 있습니다. 공에서. 위로 = x + 5; and ball.Left = y + 5; "암시 적으로 'decimal'유형을 'int'로 변환 할 수 없습니다. 명시 적 변환이 존재합니다." 이걸 어떻게 해결할 수 있습니까? –

+0

답변이 업데이트되었습니다. 지금 시도해보십시오. –

+0

x와 y를 ball로 설정하는 것을 깨달았습니다 .Top과 ball.Left는 ball.Left + = 5 명령을 사용했기 때문에 모든 타이머가 엉망이었습니다. 및 공 .Top + = 5; 이는 ball.Location = new Point (ball.Location.X +5, ball.Location.Y +5)와 동일합니다. –

관련 문제