我不太清楚我在做什么错,特别是因为我重新实现了以前工作正常的东西,特别是AStar算法寻路。我使用C#并且在Unity中工作以防万一。我以前的实现和当前的实现之间的主要区别在于,我之前将Navigation Nodes作为Unity中的独立游戏对象,现在它们只是3D数组中的类实例。我遇到的第一个意想不到的障碍是我不得不重写Equals()方法,因为这次列表<> .Contains()会正确响应被检查的不同节点(以前不需要)。覆盖有帮助,但后来我发现自己陷入了一个非常奇怪的问题(至少对我来说这一点)。这里有一块是造成我的代码的问题吧:在这里重写Equals()与null的比较失败?
public static List<Vector3> PrintPath (ref NavNode node, ref NavNode caller)
{
caller.parent = null;
List<Vector3> temp = new List<Vector3>();
NavNode tempNode = node;
Debug.Log(tempNode.parent.parent.parent.position);
while (tempNode.parent!=null) {
temp.Add (tempNode.position);
Debug.Log (tempNode.position);
tempNode = tempNode.parent;
}
temp.Add (tempNode.position);
temp.Reverse();
return temp;
}
的事情是,我敢肯定tempNode.parent不为空,即调试语句的工作在控制台很好,我可以去更深引用进一步的父母。我的代码永远不会进入while()循环(但是再一次 - 这是我在之前的实现中已经工作过的)。此外,如果我从“tempNode.parent!= null”切换到“tempNode.parent == null”,则循环开始执行,在这种情况下,它会经历所有步骤,并在层次结构中的最后一个父项上引发空引用。 下面是等于重写()我用:
public override bool Equals(System.Object obj)
{
if (obj == null)
{
return false;
}
NavNode node = (NavNode)obj;
if ((System.Object)node == null)
{
return false;
}
return (node.position.x == position.x) && (node.position.y == position.y) && (node.position.z == position.z);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
任何人有任何想法,我可能是做错了什么?花了几个小时试图弄清楚,任何帮助将不胜感激。