我的A *寻路实现没有给出预期的结果

时间:2011-08-10 12:05:46

标签: actionscript-3 path-finding a-star

我的AS3 A * pathfinding实现有时不会返回最有效的路由,而是这样:

[E][X][ ][ ][ ]
[.][X][.][.][ ]
[ ][.][ ][i][S]

(其中。是节点走路,X是墙.S =开始,E =结束,i =我想象的标记)

问题:我应该有一个总分(距离结束)30 +(从开始的距离)10 = 40,而我上面的瓷砖应该有一个总分(距离结束)40 +(距离开始)14 = 54.为什么54被选中而不是40,我不知道 - 我用它来查找开放列表中总得分最低的节点:

        var lowestTScore:int = 10000;
        var pointerTo:PathNode;
        for each (var Cur:PathNode in openList) {
            //loops through each node in openlist and finds the one with lowest total score.
            if (Cur.distS + Cur.distE < lowestTScore) {
                lowestTScore = Cur.distS + Cur.distE;
                pointerTo = Cur;
            }
        }

(我看不出任何问题。)

我想,也许我计算到最后的距离是个错误。所以我检查了我的代码:

theNode.distE = (Math.abs(theNode.xpos - endPts[0]) + Math.abs(theNode.ypos - endPts[1])) * 10;

(再次,我看不出任何问题。)

我真的很难过。

Main.as:http://pastebin.com/ZKQJwY4S PathSearcher:as:http://pastebin.com/KnmWGbQw

(我知道最好直接发布问题代码,但我不知道问题代码在哪里:(抱歉)

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

发现问题了!我没有添加

    theNode.distS = theNode.parentNode.distS + cost;

更改节点的父节点时。我只更改了parentNode,但没有更改distS分数。