Here’s a little example that shows a how a basic setup would work. You can find more information on analyzers, maps and pathrequests in the corresponding sections.
package { import be.dauntless.astar.basic2d.BasicTile; import be.dauntless.astar.basic2d.Map; import be.dauntless.astar.basic2d.analyzers.WalkableAnalyzer; import be.dauntless.astar.core.Astar; import be.dauntless.astar.core.AstarEvent; import be.dauntless.astar.core.IAstarTile; import be.dauntless.astar.core.PathRequest; import flash.display.Sprite; import flash.geom.Point; public class GeneralTest extends Sprite { private var dataMap:Array; private var map : Map; private var astar : Astar; private var req:PathRequest; public function GeneralTest() { dataMap = [ [0,0,1,1,1,0], [0,0,0,0,1,0], [0,1,1,0,0,0], [0,0,0,0,1,0], [0,1,1,0,1,0], [1,1,0,0,0,0] ]; //create a new map and fill it with BasicTiles map = new Map((dataMap[0] as Array).length, dataMap.length); for(var y:Number = 0; y< dataMap.length; y++) { for(var x:Number = 0; x< (dataMap[y] as Array).length; x++) { map.setTile(new BasicTile(1, new Point(x, y), (dataMap[y][x]==0))); } } //create the Astar instance and add the listeners astar = new Astar(); astar.addEventListener(AstarEvent.PATH_FOUND, onPathFound); astar.addEventListener(AstarEvent.PATH_NOT_FOUND, onPathNotFound); //create a new PathRequest req = new PathRequest(IAstarTile(map.getTileAt(new Point(0, 0))), IAstarTile(map.getTileAt(new Point(5, 5))), map); //a general analyzer astar.addAnalyzer(new WalkableAnalyzer()); astar.getPath(req); } private function onPathNotFound(event : AstarEvent) : void { trace("path not found"); } private function onPathFound(event : AstarEvent) : void { trace("Path was found: "); for(var i:int = 0; i<event.result.path.length;i++) { trace((event.result.path[i] as BasicTile).getPosition()); } } } } |
This will return
Path was found:
(x=0, y=0)
(x=0, y=1)
(x=0, y=2)
(x=1, y=3)
(x=2, y=3)
(x=3, y=4)
(x=4, y=5)
(x=5, y=5)
Or, graphically:

Hi!
Say please, how me create pathfind without diagonal transition?
Hi Andrew,
You have to add the FullClippingAnalyzer as documented on http://www.dauntless.be/astar/analyzers. It could be a little more clear though, so I’ll change this documentation page a little.
Thank you, very much!
But if i change analyzer on FullClippingAnalyzer, path go over ocuped tiles. Thus, the path is solved incorrectly. (
You don’t have to change it from one to another, you have to add them both:
astar.add(new WalkableAnalyzer());
astar.add(new FullClippingAnalyzer());
O! Thank you.
I am try.
how can i change a tile in retrospect, if the whole map already built???
i´d tried it with:
map.setTile(new BasicTile(1, new Point(target.x, target.y), false));
but the tile is walkable further…
That should work, although it’s probably better to request the existing tile with getTileAt and change it’s walkability.
Your problem may be that you have ‘true’ as the caching parameter for the Astar instance. When you use new Astar(true); it caches the found paths and you can only use that if you have a static (= non changing) map. If your map can change, you should use new Astar()
How do I account for dynamic obstacles, ie NPCs walking around and getting in the way?
The easiest way would be to recalculate the path when you run into an obstacle. Here’s a little more information: http://theory.stanford.edu/~amitp/GameProgramming/MovingObstacles.html
As described there, if their movement is predictable, you can hack my classes and have it give the current path length as an argument to the Map class so that it can calculate if a tile will be walkable after x steps.
Another way would be to have cooperative path finding where every unit that searches for a path is aware of the paths of all the other units, but that’s quite a big step from my current implementation.