C# ArgumentOutOfRangeException未处理
异常的进一步详细信息:索引超出范围。必须为非负数且小于集合的大小。参数名称:索引 当阅读异常时,我理解它试图告诉我什么。我不明白的是——为什么——它就要来了。以下是相关代码的片段:C# ArgumentOutOfRangeException未处理,c#,exception,xna,C#,Exception,Xna,异常的进一步详细信息:索引超出范围。必须为非负数且小于集合的大小。参数名称:索引 当阅读异常时,我理解它试图告诉我什么。我不明白的是——为什么——它就要来了。以下是相关代码的片段: //the model contains more than one mesh, so each //one must be accounted for in the final sphere List<BoundingSphere> spheres = n
//the model contains more than one mesh, so each
//one must be accounted for in the final sphere
List<BoundingSphere> spheres = new List<BoundingSphere>();
int index = 0;
//cycle through the meshes
foreach (ModelMesh mesh in this.model.Meshes)
{
//and grab its bounding sphere
spheres[index++] = mesh.BoundingSphere; //<- this is the line that throws the exception
} //end foreach
//模型包含多个网格,因此每个网格
//必须在最后一个领域考虑一个人
列表范围=新列表();
int指数=0;
//在网格中循环
foreach(此.model.mesh中的ModelMesh网格)
{
//并抓住它的边界球
球体[index++]=mesh.BoundingSphere;//您需要使用list.Add(…)而不是索引
deafult的列表大小为0,您可以添加项目,但您对不存在的项目进行编码索引。即使索引为0,它也会失败。您需要使用列表。添加(…)而不是索引
deafult将列表的大小设置为0,您可以添加项,但您可以对不存在的项进行编码索引。即使索引为0,它也会失败。您需要使用add方法来增加列表的大小。因此,请尝试spheres.add(mesh.BoundingSphere);
而不是spheres[index++]=mesh.BoundingSphere;
您需要使用Add方法来增加列表的大小。因此,请尝试spheres.Add(mesh.BoundingSphere);
而不是spheres[index++]=mesh.BoundingSphere;
您想要编写spheres.Add(mesh.BoundingSphere)
。您的球体
列表在创建后为空。您无法访问不存在的项目。您的意思是编写球体。添加(mesh.BoundingSphere)
。您的spheres
列表在创建后为空。您无法访问不存在的项。您可以将其编写为var-spheres=model.mesh.Select(m=>m.BoundingSphere.ToList();
或var-spheres=(从模型中的m.mesh选择m.BoundingSphere.ToList())
您可以将其写成var-spheres=model.mesh.Select(m=>m.BoundingSphere.ToList();
或var-spheres=(从模型中的m.mesh选择m.BoundingSphere.ToList();