2013-08-03 2 views
-4

나는 게임을 만들고 총알을 쏘는 기능이 있으며 플레이어는 총알 10 개를 얻습니다. 이것은 지금 보이는 것입니다 :내 총알 촬영 알고리즘 관련 문제

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left) 
    { 
     bullets.Add(new Bullet(robot.RobotRec)); 
     Bullet -= 1;// lose a life 
     lblBullet.Text = Bullet.ToString();// display number of lives 
     checkBullet(); 
    } 
} 
private void pnlGame_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left) 
    { 
     bullets.Add(new Bullet(robot.RobotRec)); 
     Bullet -= 1;// lose a life 
     lblBullet.Text = Bullet.ToString();// display number of lives 
     checkBullet(); 
    } 
} 
private void checkBullet() 
{ 
    if (Bullet == 0) 
    { 
     tmrShoot.Enabled = false; 

    } 
} 

그러나 총알 수가 0이되면 아무 효과가 없지만 여전히 촬영할 수 있습니다. 좋아, 그들은 단지 통과하지만 총알 이미지는 사용하지 않기를 원합니다. 그래서 나는 생각하고 있었다 : 당신은 mousedown을 해제 할 수 있습니까? 그리고 그 코드는 무엇입니까?

+2

'tmrShoot'란 무엇입니까? 그리고 "총알"은 나에게 "삶"처럼 들리지 않습니다 - 실제로 어떤 개념을 표현하려고합니까? –

+0

이 태그가 4 가지 버전의 C#으로 태그가 지정되는 이유는 무엇입니까? 무엇 이니? – JJJ

+0

정말 그렇게해서는 안되는 경우에도 글 머리 기호를 추가하는 것 같습니다. mousedown에서'Bullet'의 값을 확인하고 거기에 추가 할 것인지 결정하지 않으시겠습니까? 그게 당신 문제인가요? – Bart

답변

0

당신은 총알 == 0 잘못 경우,이 같은 뭔가 더해야한다 검사를하고있다 :

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (Bullet == 0) 
    { 
     tmrShoot.Enabled = false; 
     return; //leaves the method and followup code doesnt run 
    } 

    if (e.Button == MouseButtons.Left) 
    { 
     bullets.Add(new Bullet(robot.RobotRec)); 
     Bullet -= 1;// lose a life 
     lblBullet.Text = Bullet.ToString();// display number of lives 
    } 
} 
private void pnlGame_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (Bullet == 0) 
    { 
     tmrShoot.Enabled = false; 
     return; //leaves the method and followup code doesnt run 
    } 

    if (e.Button == MouseButtons.Left) 
    { 
     bullets.Add(new Bullet(robot.RobotRec)); 
     Bullet -= 1;// lose a life 
     lblBullet.Text = Bullet.ToString();// display number of lives 
    } 
} 

이 그 총알을 작성하고 addtional됩니다 방지 할 수는 -1 계산된다.

당신은 또한 수 있지만 방법에 이는 MouseDown 이벤트의 전체 코드는 다음과 같은 적은 코드, 가지고 :

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    FireBullet(e); 
} 
private void pnlGame_MouseDown(object sender, MouseEventArgs e) 
{ 
    FireBullet(e); 
} 

private void FireBullet(MouseEventArgs e) 
{ 
     if (Bullet == 0) 
     { 
      tmrShoot.Enabled = false; 
      return; //leaves the method and followup code doesnt run 
     } 

     if (e.Button == MouseButtons.Left) 
     { 
      bullets.Add(new Bullet(robot.RobotRec)); 
      Bullet -= 1;// lose a life 
      lblBullet.Text = Bullet.ToString();// display number of lives 
     } 
} 

또는 같은 뭔가 Form1에와 pnlGame에서 동일 MouseDown 이벤트를 등록 :

//In FormLoad 
Form1.MouseDown += FireBullet_MouseDown; 
pnlGame.MouseDown += FireBullet_MouseDown; 

private void FireBullet_MouseDown(object sender, MouseEventArgs e) 
{ 
     if (Bullet == 0) 
     { 
      tmrShoot.Enabled = false; 
      return; //leaves the method and followup code doesnt run 
     } 

     if (e.Button == MouseButtons.Left) 
     { 
      bullets.Add(new Bullet(robot.RobotRec)); 
      Bullet -= 1;// lose a life 
      lblBullet.Text = Bullet.ToString();// display number of lives 
     } 
}