Bullet == 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
}
}
これにより、Bullet が作成され、追加の -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
}
}