【已解决】初学,写的a*寻路代码会导致精灵位置错误……

昨天照着教程自己写了个A*寻路的代码,编译之后发现只要把代码放到工程里就会导致精灵位置错误……不管是否调用……

tiledMap层里的精灵的位置全变成了-107374176.000000, -107374176.000000

头文件:


#include "cocos2d.h"
using namespace cocos2d;

#include <vector>
using namespace std;

std::vector<cocos2d::Vec2> getRoute(Vec2 A, Vec2 B, vector<Vec2> blockagePos);

bool isWalkable(Vec2 p,    vector<Vec2> blockagePos);

class Step
{
public:
Vec2 position;
int GScore;
int HScore;
int FScore;
Step* parent;

bool openList;
bool closeList;
};


cpp文件:


#include "Astar.h"
#include <vector>
using namespace std;

using namespace cocos2d;

USING_NS_CC;

vector<Vec2> getRoute(Vec2 A, Vec2 B, vector<Vec2> blockagePos){

    vector<Vec2> route;
    vector<Step> pool;
    
    //开始位置
    Step start;
    start.position = A;
    start.GScore = 0;
    start.HScore = abs(A.x - B.x) + abs(A.y - B.y);
    start.FScore = start.GScore + start.HScore;
    start.openList = false;
    start.closeList = true;
    
    Step* currentStep;
    Vec2 cP = A;
    
    for (; 1;)
    {
        //1.从OpenList里选出F值最小的
        for (Step i : pool){
            if (i.FScore < currentStep->FScore) currentStep = &i;
        }
        
        if (currentStep->position == B) {
            //路线存进route
            for (; 1;){
                route.push_back(currentStep->position);
                currentStep = currentStep->parent;
                if (currentStep->position == A) return route;
            }
        }

        //2.将最小的从open列表移除,然后添加到closed列表中。
        currentStep->closeList = true;
        currentStep->openList = false;
        
        //3.对于相邻的每一块可通行的方块:
        //        如果在closed列表中:不管它。
        //        如果不在open列表中:添加它然后计算出它的和值。
        //        如果已经在open列表中:当我们使用当前生成的路径到达那里时,检查F值是否更小。如果是,更新它的和值和它的前继。
        cP = currentStep->position;

        //右
        if (isWalkable(ccp(cP.x + 1, cP.y), blockagePos)) {
            bool isInPool = false;
            for (Step i : pool){
                if (i.position == ccp(cP.x + 1, cP.y) && i.closeList == true) isInPool = true;//在closed列表中,不管它
                if (i.position == ccp(cP.x + 1, cP.y) && i.openList == true){
                    isInPool = true;
                    if (currentStep->GScore + 1 + i.HScore < i.FScore){
                        //更新值和前继
                        i.FScore = currentStep->GScore + 1 + i.HScore;
                        i.parent = currentStep;
                    }
                }
            }
            //不在池里
            if (!isInPool){
                //初始化并加入openList
                Step step;
                step.position = ccp(cP.x + 1, cP.y);
                step.parent = currentStep;
                step.GScore = step.parent->GScore + 1;
                step.HScore = abs(cP.x + 1 - B.x) + abs(cP.y - B.y);
                step.FScore = step.GScore + step.HScore;
                step.closeList = false;
                step.openList = true;
                pool.push_back(step);
            }
        }//endif
               
                //还有三个方向,都一样,删了省地方……
        
    }//endfor
    
}


bool isWalkable(Vec2 p, vector<Vec2> blockagePos){

    for (Vec2 i : blockagePos){
        if (i == p) return false;
    }
    return true;

}

没有人QAQ……

把教程的代码全盘抄上也会出同样的问题:10:
可只要注释掉A*cpp里的代码就恢复正常……

把代码放进去,单步调试一下,看看什么时候坐标值被改变了,这种问题,很多时候不得不自己动手,请楼主加油吧

感谢了,刚知道还有单步调试这个功能:12:,之前都是一点一点把代码注释掉试……

:12:解决了……
用了绝对值函数,头文件没包含math.h