2

我正在尝试用 C#(windows 形式)制作一个迷你游戏,比如太空入侵者。飞船飞得很好,但我没有得到多张照片,我有一种timer_Tick制作动画的方法,当我按空格拍摄时,第一张效果很好,但第二张效果很好。在这个半位置的第一枪消失了。镜头是pictureBox,我尝试了 List of pictureBox,但没有成功。

我有这样的事情:

    public void Form1_Load(object sender, EventArgs e)
    {
        shootP = new PictureBox();
        shootP.Image = Properties.Resources.shoot_1;
        shootP.SizeMode = PictureBoxSizeMode.Zoom;
        shootP.Size = new Size(10, 72);  
        ListShoot = new List<PictureBox>();      
        int i = -1;
    }
     private void Form1_KeyDown(object sender, KeyEventArgs e)
     {

        if (e.KeyCode == Keys.Space)
        {
        control = false;
        i++;
        ListShoot.Add(shootP);
        timeShoot.Start();
     }

      private void timeShoot_Tick(object sender, EventArgs e)
      {
        if (control == false)
        {   
            ListShoot[i].Location = new Point(spacecraft._imageBox.Location.X + 50, spacecraft._imageBox.Location.Y - 55);  // align the shoot with spacecraft
            control = true;     
        }

            ListShoot[i].Top -= 40;
      }

我究竟做错了什么?

4

2 回答 2

3

您每次都需要创建一个新图像,就像在 aliassce 的答案中一样。

private void Form1_KeyDown(object sender, KeyEventArgs e)
 {
    if (e.KeyCode == Keys.Space)
    {
      control = false;
      shootP = new PictureBox();
      shootP.Image = Properties.Resources.shoot_1;
      shootP.SizeMode = PictureBoxSizeMode.Zoom;
      shootP.Size = new Size(10, 72);
      ListShoot.Add(shootP);
      timeShoot.Start();
    }
 }

然后您需要更新列表中所有图像的位置。使用 foreach 循环并放弃 int 迭代器。

private void timeShoot_Tick(object sender, EventArgs e)
  {
    if (control == false)
    {   
       foreach PictureBox shot in ListShot
       {
           shot.Location = new Point(spacecraft._imageBox.Location.X + 50, spacecraft._imageBox.Location.Y - 55);  // align the shoot with spacecraft
           shot.Top -= 40;
       }
       control = true;
    }
  }

您可能还必须更新绘制图像的代码以循环遍历列表。有类似的东西:

foreach PictureBox shot in ListShoot
{
   draw(shot);
}

最后,您可能需要时不时地清理列表,以摆脱不再出现在屏幕上且不再需要更新或绘制的任何镜头。

于 2012-10-20T21:52:17.750 回答
1

您正在添加相同的 PictureBox。您可以尝试创建一个新的。将此圆锥添加到Form1_KeyDown.

    shootP = new PictureBox();
    shootP.Image = Properties.Resources.shoot_1;
    shootP.SizeMode = PictureBoxSizeMode.Zoom;
    shootP.Size = new Size(10, 72);

另一方面,您应该尽快使用 OOP

于 2012-10-20T19:48:00.193 回答