我试图在简单的四向移动网格上围绕A Star Pathfinding构建AI,但每当我尝试将节点添加到比较中字典和排序它们在Dictionary.add上崩溃。我不完全确定为什么在这方面,任何人都可以发现缺陷?A *寻路AI在向节点排序的字典添加节点时冻结
void DirectionFinder()
{
// Find Target Point //
GetBoardPosition();
BuildOrReBuildPawnRefBoard();
bool IsTrue = false;
int Serial = lastSquare.Location.Serial;
// Get the Serial Keys of the nodes that are neighboring this node //
bool TestNode = NodesClosed.ContainsKey(Serial);
Dictionary<int, Node> SortArray = new Dictionary<int, Node>();
SortArray.Clear();
if (TestNode)
{
Node ScanNode = NodesClosed[Serial];
// Get the Number of Neighbors I have //
int TestNumNeighbors = ScanNode.NeighborNodes.Count;
// Set up a Loop that will go through these nodes and get the lowest-scored movement node //
for (int loop = 0; loop < TestNumNeighbors; loop++)
{
int ScanNodePointX = ScanNode.NeighborNodes[loop].X;
int ScanNodePointY = ScanNode.NeighborNodes[loop].Y;
int ScanTestSerial = System_GameController.Instance.NodeArray[ScanNodePointX, ScanNodePointY].Location.Serial;
bool IsNodeOpen = NodesOpen.ContainsKey(ScanTestSerial);
if (IsNodeOpen)
{
Debug.Log("This Node is Open:" + ScanTestSerial);
Node CompareNode = NodesOpen[ScanTestSerial];
int CostOfNode = CompareNode.TotalCost;
SortArray.Add(CostOfNode, CompareNode);
}
}
}
}
它一直下降到“CostOfNode”。这是有效的....然后当它试图添加到SortArray时它会中断。
查看Eric Lippert的C#A-star here:http://blogs.msdn.com/b/ericlippert/archive/2007/10/10/path-finding-using-a-in-c-3 -0-part-four.aspx – 2014-09-01 03:04:53