2017-10-14 35 views
0

我试图使用数组来存储入侵者的空间入侵者游戏,但每次运行代码它冻结,并有我的数组是错误的大小的错误。添加多个pictureboxes从阵列的C#中形成

错误:

An unhandled exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll

任何人都可以请帮助?以下是我的代码:

class invaders 
    { 
     ingame game = new ingame(); 
     PictureBox[] spaceinvaders = new PictureBox[100]; 
     public void spawn(int level) 
     { 
      PictureBox invader = new PictureBox(); 
      Bitmap img = (WindowsFormsApplication1.Properties.Resources.SpaceInvader); 
      for (int n = 1; n == 3 + level; n++) 
      { 
       for (int i = 12; i == 493; i = i + 37) 
       { 
        invader = new PictureBox(); 
        invader.Size = new Size(12, 12); 
        invader.Image = img; 
        spaceinvaders[i] = invader; 

        spaceinvaders[i].Location = new Point(i, n); 
       } 
      } 
      game.Controls.AddRange(spaceinvaders); 
     } 
    } 
+0

请给我们具体的错误信息 – Wndrr

+0

在mscorlib.dll中发生未处理的异常'System.OutOfMemoryException' – space482

+0

在哪一行? – Wndrr

回答

0

您的for循环的条件是错误的。您需要使用<符号而不是==。中间表达式将针对每次迭代进行检查,并且只有当检查输出为true时循环才会继续执行。

由于您要求内循环从12493,它会这样做。除了493大于100,因此数组在界限之外被访问,所以你应该得到一个“数组越界”异常。

我无法重现您的错误。请修复您的代码,使其可重现。

+0

如何使其具有可重现性? – space482

+0

我得到的错误更改为“mscorlib.dll中发生类型'System.StackOverflowException'的未处理的异常”,说我需要确保我没有通过将所有“==”更改为无限循环“<=” – space482

+0

我最后输入错了。有用 – space482