2014-04-25 18 views
1

我使用easystarjs https://github.com/prettymuchbryce/easystarjs並從頁面上的示例開始。如何儘可能跟隨easystarjs

var grid = [[0,0,1,0,0], 
      [0,0,1,0,0], 
      [0,0,1,0,0], 
      [0,0,1,0,0], 
      [0,0,1,0,0]]; 

//startX, startY ,endX, endY, callback   
easystar.findPath(0, 3, 4, 3, function(path) { 
      path = path || []; 
      for(var i = 0, ilen = path.length; i < ilen; i++) { 
       //console.log(path[i].x, path[i].y); 
       marker.drawRect(path[i].x*32, path[i].y*32, 32, 32); 
     } 

    }); 

如果我運行的代碼,沒辦法吸引,因爲它是不完整的(有一個在路上一個號碼的牆)。是否有可能修改代碼,以便代替它只是說找不到路徑(或沒有繪圖),我想讓代碼儘可能地繪製(到牆上)。

如果我將第一個數字更改爲數字零(並創建一段文字),代碼將起作用。

回答

0

該功能將返回一個不同的結果,具體取決於您轉向0的哪個1,因此「儘可能」與此相關。

只是讓這個路徑是否空你填寫這樣說:

currentX = startX; 
currentY = startY; 
path = []; 

while(grid[currentY][currentX] != 1){ 
    path.push({x: currentX, y: currentY}); 
    dX = endX - currentX; 
    dY = endY - currentY; 
    distanceX = Math.abs(dX); 
    distanceY = Math.abs(dY); 
    directionX = dX/distanceX; 
    directionY = dY/distanceY; 
    // Make a step in the direction where the distance is bigger 
    if(distanceX > distanceY){ 
    currentX += directionX; 
    }else{ 
    currentY += directionY; 
    } 
} 

這將是一種直線通過壁打斷目標的。