To find a path, you must follow these steps:
- Set the Grid: Provide a two-dimensional array representing your map.
- Set Acceptable Tiles: Define which tile values are walkable.
- Request a Path: Call
findPath(startX, startY, endX, endY, callback). The callback receives the path (an array of points) or null if no path is found. - Trigger Calculation: Crucially, EasyStar does not calculate automatically. You must call
easystar.calculate() (ideally on a ticker or setInterval) to process the pathfinding asynchronously.
To prevent performance issues on large grids, you can limit the work done per tick using setIterationsPerCalculation(value).
var grid = [[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,0,0,0]];
easystar.setGrid(grid);
easystar.setAcceptableTiles([0]);
easystar.findPath(0, 0, 4, 0, function( path ) {
if (path === null) {
alert("Path was not found.");
} else {
alert("Path was found. The first Point is " + path[0].x + " " + path[0].y);
}
});
// You must call this to start the asynchronous calculation
easystar.calculate();