C#返回结构的新实例会抛出ArrayOutOfRange异常

时间:2016-06-26 12:56:25

标签: c#

您好我有以下功能:

    private BspFile.dleaf_t GetLeafForPoint(_vec3 point)
    {
        var node = 0;

        BspFile.dnode_t pNode;
        BspFile.dplane_t pPlane;

        var d = 0.0f;

        while (node >= 0)
        {
            pNode = Nodes[node];
            pPlane = Planes[pNode.planenum];

            d = point.x*pPlane.normal.x + point.y*pPlane.normal.y + point.z*pPlane.normal.z - pPlane.dist;

            node = d > 0 ? pNode.children[0] : pNode.children[1];
        }

        if ((-node - 1) >= 0 && -node - 1 < Leafs.Length)
        {
            return Leafs[-node - 1];
        }
        else
        {
            BspFile.dleaf_t newLeaf = new BspFile.dleaf_t();

            newLeaf.area = -1;
            newLeaf.contents = 0;
            return newLeaf;
        }

    }

如果我运行它,则以下行抛出一个ArrayOutOfIndex异常: 返回newLeaf;

我不知道为什么因为struct实例已经初始化并且某些值已经设置了......

感谢您的帮助

0 个答案:

没有答案