2014-08-30 59 views
1

当我尝试在程序中拍摄彩弹时,出现上述问题。类(Paintballs)代码如下。未处理的异常。指数超出范围。必须是非负数且小于集合的大小。参数名称:索引

class Paintballs 
{ 
    public List<Point> myClick; 

    public Paintballs() 
    {    
     myClick = new List<Point>(); 
    } 

    public void add(Point location) 
    { 
     myClick.Add(location);      
    } 

    public void paint(Graphics g) 
    { 
     foreach (Point p in myClick) 
     { 
      g.FillEllipse(Brushes.Blue, p.X, p.Y, 20, 20); 
     }    
    } 

    public Point getPoints(int hit) 
    { 
     return myClick[hit]; 
    }  
} 

当我调试程序,错误是在getPoints方法“返回了MyClick哈工大]就行了。下面是我的Form1上。

namespace AmazingPaintball 
{ 
    public partial class Form1 : Form 
    { 
     Random positionX = new Random(); 
     Random positionY = new Random(); 
     Target einstein; 
     int count = 0; 
     Paintballs pBalls = new Paintballs(); 
     Stopwatch stopwatch = new Stopwatch(); 

     //Foreach loop 
     //Draw paintballs first 
     //then target 

     SoundPlayer wavPlayer = new SoundPlayer(); 
     public Form1() 
     { 
      InitializeComponent(); 
      Point point = new Point(positionX.Next(0, 638), positionY.Next(0, 404)); 
      einstein = new Target(point); 
      ptrEinstein.Location = point; 
     } 

     private void pictureBox1_Paint(object sender, PaintEventArgs e) 
     { 
      pBalls.paint(e.Graphics); 
     } 

     private void Form1_KeyDown(object sender, KeyEventArgs e) 
     { 
      ptrEinstein.Location = einstein.Move(e.KeyData); 
      pictureBox1.Update(); 
      pictureBox1.Refresh();  
     } 

     private void pictureBox1_MouseClick(object sender, MouseEventArgs e) 
     {    
      pBalls.add(e.Location); 
      pictureBox1.Refresh(); 
      count++; 

      Point shotHit = pBalls.getPoints(count); 

      if (ptrEinstein.Location == shotHit) 
      { 
       stopwatch.Stop(); 
       MessageBox.Show("It took " + count + " shots and " + stopwatch.Elapsed + " seconds to hit the target"); 
      }    
     }  

     private void Form1_Load(object sender, EventArgs e) 
     { 
      stopwatch.Start(); 
     }   
    } 
} 

在Form1,这个问题似乎是。在picturebox1_MouseClick事件的count可变我不知道我是否应该保持在0或1,请让我知道,谢谢

+2

尝试增量次数。看起来你试图让总索引大于数组大小。由于这个特殊的例子,你可以在List中使用Last()方法。 – 2014-08-30 15:03:08

回答

0

计数++; 点shotHit = pBalls.getPoints(计数);

这是错误的 您必须在countBits.getPoints(count-1)中传递-1而不是-1。

或 增量之后你会得到shotHit

+0

它的工作感谢你 – ChefBrohan 2014-08-30 15:05:44

+0

感谢您更新结果。如果有效,请将其标记为已回答 – 2014-08-30 15:16:34

1

的问题是你增加计调用pBalls.getPoints(计数)前。让我们来看看您的程序在第一次与它进行交互时的作用:

  1. 触发picturebox1_MouseClick事件。
  2. pBalls点列表添加了一个元素,现在只有一个元素(在索引0处)与鼠标位置的点相关。
  3. 计数从0递增到1.
  4. 您的程序运行pBalls.getPoints(count);但count为1. pBalls点列表只有一个元素,它位于索引0中。myClick [1]正在被调用,但您正在寻找myClick [0],这是您刚添加的点。

您可以递增计数调用pBalls.getPoints(计数)或通过与pBalls.getPoints(计数1)真正的索引值;

0

问题发生在以下情况。让我们假设我们有一个列表。

//创建字符串类型

List<string> lstString = new List<string>(); 

//的列表在列表

for(int i = 0 ; i<3 i++) 
{ 
lstString.Add("number" + i); 
} 

现在字符串的长度为3添加3个值。

当你写

lstString[0] it gives you number0 
lstString[1] // // // number1 
lstString[2] // // // number2 

,但如果你写

lstString[3] 

它抛出异常,因为你通过不列表中出现的索引。换句话说,索引号大于lstString中的索引。

这正是你在这里做什么

private void pictureBox1_MouseClick(object sender, MouseEventArgs e) 
     {    
      pBalls.add(e.Location); // you add element at index 0 
      pictureBox1.Refresh(); 
      count++; // count++ mean count = 1; 

      // now you passing 1 but the list only has zero index right. 
      Point shotHit = pBalls.getPoints(count); 

      if (ptrEinstein.Location == shotHit) 
      { 
       stopwatch.Stop(); 
       MessageBox.Show("It took " + count + " shots and " + stopwatch.Elapsed + " seconds to hit the target"); 
      }    
     }  

解决方案:让shotHit后

private void pictureBox1_MouseClick(object sender, MouseEventArgs e) 
      {    
       pBalls.add(e.Location); // you add element at index 0 
       pictureBox1.Refresh();   

       Point shotHit = pBalls.getPoints(count); 

       if (ptrEinstein.Location == shotHit) 
       { 
        stopwatch.Stop(); 
        MessageBox.Show("It took " + count + " shots and " + stopwatch.Elapsed + " seconds to hit the target"); 
       } 

       count++; // increment at the end of the method.   
      }  
相关问题