2014-05-04 45 views
0

第一行打印,比方說,10,而第二個將打印少1:Javascript:console.log(arr.length)表示長度爲n,console.log(arr)表示長度爲n-1。第二種情況下不會顯示最後一個元素

console.log("ds length: " + ds.length); // Prints, say, 10 
console.log(ds); // Prints all but last element and says length is one less. 

一些相關的我的部分代碼,所有的單AS:

FindPath: function(oX, oY, dX, dY) { // origin, destination 
     var heap = AS.Heap(); 

     var g = AS.Grid; // AS.Grid is declared, in AS, as: Grid: new Array(), 

     var node; 

     var cn = g[oX][oY]; // current node 
     var dn = g[dX][dY]; // destination node 

     heap.push(cn); 

     cn.CoorType = AS.CoorType.VISITED; 

     while (heap.length > 0) { 
      cn = heap.pop(); 

      if (cn === dn) { 
       var ds = new Array(); // direction set (instructions) 
       var pn;// parent node; 

       do { 
        pn = cn.Parent; 
        ds.push({ 
         Dir: cn.Y - pn.Y !== 0 ? cn.Y - pn.Y < 0 ? AS.Direction.UP : AS.Direction.DOWN : cn.X - pn.X > 0 ? AS.Direction.RIGHT : AS.Direction.LEFT, 
         Node: cn}); 
        cn = pn; 
       } while (pn.Parent); 

       console.log("ds length: " + ds.length); 
       console.log(ds); 

       AS.CleanUp(); 
       return ds; 
      } 

        // Bellow, I'm not using functions 'cause occasionally, when FindPath() is called multiple times in a large area within a few milliseconds, it will get laggy. I removed the use of functions to increase performance. 
      node = g[cn.X+1][cn.Y]; 
      if (node.CoorType === AS.CoorType.NOTHING) { 
       node.CoorType = AS.CoorType.VISITED; 
       node.Parent = cn; 
       node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) + 
         Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001; 
       heap.Sink(node); 
      } 
      node = g[cn.X][cn.Y+1]; 
      if (node.CoorType === AS.CoorType.NOTHING) { 
       node.CoorType = AS.CoorType.VISITED; 
       node.Parent = cn; 
       node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) + 
         Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001; 
       heap.Sink(node); 
      } 
      node = g[cn.X-1][cn.Y]; 
      if (node.CoorType === AS.CoorType.NOTHING) { 
       node.CoorType = AS.CoorType.VISITED; 
       node.Parent = cn; 
       node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) + 
         Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001; 
       heap.Sink(node); 
      } 
      node = g[cn.X][cn.Y-1]; 
      if (node.CoorType === AS.CoorType.NOTHING) { 
       node.CoorType = AS.CoorType.VISITED; 
       node.Parent = cn; 
       node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) + 
         Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001; 
       heap.Sink(node); 
      } 
     } 

     AS.CleanUp(); 
     return heap; // No path found 
    }, 

Heap: function() { 
    var heap = new Array(); 

    heap.Sink = function(node) { 
     var i = this.length-1; 

     while (i > 0 && this[i].Score < node.Score) i--; 

     this.splice(i, 0, node); 
    }; 

    return heap; 
}, 

CleanUp: function() { // Cleans up changes made by any previous path search 
    var x, y, g = AS.Grid; 

    var limX = g.length - 3; 
    var limY = g[0].length - 3; 

    for (x = 2; x < limX; x++) { 
     for (y = 2; y < limY; y++) { 
      if (g[x][y].CoorType === AS.CoorType.VISITED) { 
       g[x][y].CoorType = AS.CoorType.NOTHING; 
       delete g[x][y].Parent; 
      } 
     } 
    } 
} 

我意識到,有時,我應該根據FindPath返回的路徑移動的對象將錯過一步。然後我看到了我上面描述的內容。

enter image description here

回答

1

通過在DO {添加兩行}而(pn.Parent):

do { 
    pn = cn.Parent; 
    ds.push({ 
     Dir: cn.Y - pn.Y !== 0 ? cn.Y - pn.Y < 0 ? AS.Direction.UP : AS.Direction.DOWN : cn.X - pn.X > 0 ? AS.Direction.RIGHT : AS.Direction.LEFT, 
     Node: cn}); 
    console.log(ds.length); // Added this line, 
    console.log(ds);  // and this one 
    cn = pn; 
} while (pn.Parent); 

我意識到的console.log(DS)中的溶液印刷DS,因爲它是一個後調用GameLoop(),在某些時候,它會調用AS.FindPAth,而console.log(ds.length)正在打印ds.length,因爲它是在我要求打印的時候打印的。由於在GameLoop()結束之前,我刪除了路徑的最後一個元素,它返回了我的FindPath()(nextDirection = path.pop()),console.log(ds)會打印自己缺少一個元素。

儘管如此,JavaScript在打印對象的狀態時會變得一團糟,因爲它會在以後的時間,而不是像在console.log()時那樣...

相關問題