A星寻路算法 cocos2d-js

把A星算法研究了下,自己用cocos2d-js实现了一次,代码如下:


效果如图A星寻路算法 cocos2d-js_第1张图片

A星寻路算法 cocos2d-js_第2张图片

var layer = new cc.LayerColor(cc.color(0, 0, 0, 127), cc.winSize.width, cc.winSize.height);
// var layer = new cc.LayerColor(cc.color(0, 0, 0, 127), cc.winSize.width, cc.winSize.height);
this.addChild(layer);
var label1 = new cc.LabelTTF('hello world', "Arial", 38);
label1.setPosition(cc.winSize.width/2, cc.winSize.height/2);
label1.setColor(cc.color(0,200,200));
label1.setString("winSize.width, cc.winSize.height =  " + cc.winSize.width + "  " + cc.winSize.height);
this.addChild(label1);

var MapAtr = [
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],//5
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[1],[1],[1],[1],[1],[1],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],//10
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
    [[0],[0],[0],[0],[0],[0],[0],[0],[0]],
];
var MapNode = [];
var openList = [];
var closeList = [];
var resultList = [];

var index = 0;
for(var i=0; i<16; i++){
    for(var j=0; j<9; j++){
        var length = MapNode.length;
        var _layer = new cc.LayerColor(cc.color(255/25*i, 255/25*j, 255/25*(i+j)/2), cc.winSize.width/16, cc.winSize.height/9);
        _layer.setPosition(i*cc.winSize.width/16, cc.winSize.height/9*j);
        _layer.setName('layer' + (i*100 + j));
        if(MapAtr[i][j][0]){
            _layer.setColor(cc.color(0,0,0));
        }
        var userData = {};
        userData.i=i;
        userData.j = j;
        userData.from=false;
        userData.end=false;
        userData.F = 0;
        userData.G = 0;
        userData.label = '';
        userData.color = cc.color(255/25*i, 255/25*j, 255/25*(i+j)/2);
        userData.arrived = false;
        _layer.setUserData(userData);
        this.addChild(_layer);
        var _label = new cc.LabelTTF('', "Arial", 20);
        _layer.addChild(_label);
        _label.setName('stringLabel');
        // _label.setString(index++);
        _label.setTag(i+100+j);
        _label.setPosition(cc.winSize.width/16/2, cc.winSize.height/9/2);
        // _label.setString((i*100+j).toString());
        MapNode[length] = _layer;
    }
}

var StartNode = null;
var EndNode = null;
var chupaiListener = cc.EventListener.create({
    event: cc.EventListener.TOUCH_ONE_BY_ONE,
    swallowTouches: true,
    onTouchBegan: function (touch, event) {
        if(!StartNode){
            for(var i=0; i



你可能感兴趣的:(cocos2d-js)