我試圖在簡單的四向移動網格上圍繞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