I needed a pathfinding algorithm for a tilebased game, and the best option was the A* algorithm. Unfortunately there isn't one out there for flash, so I made one. This algorithm will find the shortest path between two points in a grid of tiles where the tiles can either be walkable or unwalkable. It implements a min-heap, so it's quite efficient.
Demo
AStarPathfinding.zip
The following code shows you how to use the pathfinder. The map has to be a 2D array of objects, where the objects need to have the property walkable.
It returns an array of Node objects in the order the tiles should be walked. Each Node has an x and y variable which you can use to get the next tile in the path.Code:import pathfinding.*; var map:Array = [[{walkable:true}, {walkable:false}, {walkable:true}], [{walkable:true}, {walkable:true}, {walkable:true}]]; var pfinder:Astar = new Astar(map); var sx:Number = Math.round(game.startPos._x/32); var sy:Number = Math.round(game.startPos._y/32); var gx:Number = Math.round(game.stopPos._x/32); var gy:Number = Math.round(game.stopPos._y/32); var path:Array = pfinder.find(sx,sy,gx,gy);
The demo demonstrates how the pathfinding is performed. The numbers you see indicate the distance from start to finish. The red tiles indicate the path the algorithm found.
If you find any bugs or have any suggestions/questions, then reply here, and I'll see what I can do.




Reply With Quote