2017-01-20 1 views
-1

기본적으로 내 프로그램에서 사용중인 이미지의 공룡은 왼쪽을 향하고 있지만 사용자가 오른쪽 키를 누르면 오른쪽으로 뒤집어 야하고 오른쪽 키를 누르면 반복적으로 오른쪽으로 이동해야합니다. 공룡이 오른쪽을 향한 상태에서 왼쪽 키를 누르면 왼쪽으로 뒤집어 야하고 왼쪽 키를 누르면 공룡은 왼쪽으로 움직여야합니다. 다음 코드를 시도했지만 작동하지 않습니다.한 번만 반복하는 그림 상자

private void moveDinos(object sender, KeyEventArgs e) 
    { 
     bool lturn = false; 

     if (e.KeyCode == Keys.Left && titlePic.Visible == false) 
     { 
      if (lturn == true) 
       dino.Image.RotateFlip(RotateFlipType.Rotate180FlipY); 
      dino.Left -= 100; 
     } 
     else if (e.KeyCode == Keys.Right && titlePic.Visible == false) 
     { 
      if (lturn == false) 
       dino.Image.RotateFlip(RotateFlipType.Rotate180FlipY); 
      dino.Left += 100; 
     } 
    } 

나는 많은 것을 시도했지만 내 마음에서 논리를 벗어나는 것처럼 보이지 않습니다.

+0

: 같은

뭔가? 왜냐하면 지금처럼 그것은 상태 (왼쪽 또는 오른쪽으로 돌렸 읍니다)처럼 보이고 그것을 거짓으로 설정하고 다시 변경하지 않기 때문입니다. 그리고 titlePic은 당신에게 보이지 않을 수도 있지만 visible == false는 어디서나 보이지 않는 것을 의미합니다. – EpicKip

+0

키 누르기에 문제가 발생하지 않도록하려면 키 업 또는 키 다운을 사용하십시오. 따라서 키를 누르고 있으면 키 수만큼 계산됩니다. – EpicKip

+0

@EpicKip lturn이 맞으면 왼쪽으로 돌아갈 수 있음을 의미합니다. 그리고 titlePic에 관한 문제는 없습니다. –

답변

0

귀하의 lturn 부울은 디노의 방향을 결정하는 것입니까?

private bool _lturn = true; 
이 이 항상 거짓되지 않으므로 그런 다음 논리에서 사용할 수 있습니다

가 :

private void moveDinos(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Left && titlePic.Visible == false) 
     { 
      if (_lturn != true) 
      { 
       dino.Image.RotateFlip(RotateFlipType.Rotate180FlipY); 
       _lturn = true; 
      } 
      else 
      { 
       dino.Left -= 100; 
      } 
     } 
     else if (e.KeyCode == Keys.Right && titlePic.Visible == false) 
     { 
      if (_lturn == true) 
      { 
       dino.Image.RotateFlip(RotateFlipType.Rotate180FlipY); 
       _lturn = false; 
      } 
       else 
      { 
       dino.Left += 100; 
      } 
     } 
    } 

그래서 문은 또한 변경 한 경우이 방법에서 그리고 클래스로 가야합니다 :

  1. 방향이 변경되면 _lturn은 변경되지만 디노는 이동하지 않습니다.
  2. 디노가 이미 키를 누르는 방향으로 향하고 있다면, 그는 그 방향으로 움직일 것입니다.
0

직면 한 현재면을 추적해야합니다. (필드에 저장) 사용자가 왼쪽을 다시 누르면 다시 넘겨지지 않습니다. 당신이 lturn을 설정하고이 무엇을 할

private bool _facingLeft; // initial state of the image must match with this boolean. 

private void moveDinos(object sender, KeyEventArgs e) 
{ 
    // this shoudn't depend on the visible state of the title!!! you should change it. 
    if(titlePic.Visible) 
     return; 

    // check the keys if the left or right key is pressed. 
    if((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Right)) 
    { 

     // when the left button is pressed, it should face left.. 
     bool moveToTheLeft = (e.KeyCode == Keys.Left); 

     // if the current state of the image facing the wrong direction? 
     if(_facingLeft != moveToTheLeft) 
     { 
      // if not, flip the image. 
      dino.Image.RotateFlip(RotateFlipType.Rotate180FlipY); 
      // store the current facing. 
      _facingLeft = moveToTheLeft; 
     } 
     else 
      // move the dino.. 
      if(moveToTheLeft) 
       dino.Left -= 100; 
      else 
       dino.Left += 100; 
    } 

}