曾经写过A*寻路算法的教程,但没有贴出任何代码,这次代码全都贴出,大家可以进一步学习
我只是按照A*的基本思想,将A*寻路算法用AS3实现了一下,没有做太多的寻路优化等
如果你本身已经会用A*寻路算法了,那么下面的代码也不必研究了
代码中注释很多,就不贴过多的解释
看代码以前,可以先看看下面这两篇文章,或者看代码的时候配合例子和教程来看
A*(A星)寻路算法讲解
A*寻路算法,DEMO展示
在DEMO展示中,有三个版本,由于代码写了很久了,我也不记得下面贴出的代码是哪个版本的了,见谅。。
首先是文档类Index.as:
package code{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
public class Index extends Sprite{
private var road:Road;
public function Index(){
stage.align = "TL";
stage.scaleMode = "noScale";
stage.showDefaultContextMenu = false;
init();
}
//初始化
private function init():void{
road = new Road;
addChild(road);
road.x = GV.ROAD_INIT_X;
road.y = GV.ROAD_INIT_Y;
//地图规格申明
var text:TextField = new TextField;
text.htmlText = "地图规格:50*50方格,障碍物500方格 寻路算法:A*(星) 制作:sunbright";
addChild(text);
text.x = 25;
text.y = 530;
text.width = 500;
text.selectable = false;
text.mouseEnabled = false;
text.setTextFormat(new TextFormat("宋体",12,0xffffff));
text = null;
}
}
}
定义参数的类:GV.as
package{
public class GV{
public function GV(){
throw new Error("变量");
}
//Road创建的初始x、y坐标
public static const ROAD_INIT_X:int = 25;
public static const ROAD_INIT_Y:int = 25;
//纵横方块参数
public static const WIDTH_NUMBER:int = 50;
public static const HEIGHT_NUMBER:int = 50;
public static const WIDTH_TOTAL:int = 500;
public static const HEIGHT_TOTAL:int = 500;
public static const GRID_WIDTH:int = 10;
public static const GRID_HEIGHT:int = 10;
//障碍物个数
public static const THING_NUMBER:int = 500;
//state状态参数表示
public static const HAVE_THING:int = 1;
public static const IMPASSE_VALUE:int = 2;
public static const MAIN_VALUE:int = 8;
//路径耗费固定值
public static const BIAS_VALUE:int = 14;
public static const LINE_VALUE:int = 10;
//文本表示
public static const IMPASSE:String = "死路!";
public static const RESULT_FRONT:String = "用时 ";
public static const RESULT_BACK:String = " 毫秒";
}
}
主要算法所存在的Road.as类
package code{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.clearInterval;
import flash.utils.getTimer;
import flash.utils.setInterval;
public class Road extends Sprite{
private var lands:Sprite;
private var things:Sprite;
private var c:Coordinate;
private var main:Main;
private var click:Sprite;
private var drawPath:Sprite;
private var result:TextField;
private var unlockList:Array;
private var lockList:Object;
private var targetIX:int;
private var targetIY:int;
private var intervalID:int = 0;
private var startTime:int;
private var endTime:int;
public function Road(){
init();
}
//初始化
private function init():void{
//创建坐标
c = new Coordinate;
//创建文本框
result = new TextField;
result.mouseEnabled = false;
result.autoSize = "left";
result.y = -20;
result.selectable = false;
result.defaultTextFormat = new TextFormat("宋体",12,0xffffff);
addChild(result);
result.text = "结果";
//创建平原
lands = new Sprite;
lands.mouseChildren = false;
lands.mouseEnabled = false;
addChild(lands);
lands.graphics.beginFill(0xff0000);
lands.graphics.lineStyle(0);
lands.graphics.drawRect(0,0,GV.WIDTH_TOTAL,GV.HEIGHT_TOTAL);
for(var i:int = 1; i = GV.WIDTH_NUMBER || iy >= GV.HEIGHT_NUMBER){
return null;
}
//是否有障碍物
if(c[ix][iy].isThing){
return null;
}
//是否已经加入关闭标记列表
if(lockList[ix + "_" + iy]){
return null;
}
//是否已经加入开启标记列表
if(unlockList[ix + "_" + iy]){
return null;
}
//判断当斜着走的时候,它的上下或者左右是否有障碍物
if(p){
if(c[_p.ix][iy].isThing || c[ix][_p.iy].isThing){
return null;
}
}
var cx:Number = Math.abs(targetIX - ix);
var cy:Number = Math.abs(targetIY - iy);
return new Sign(ix,iy,
p ? GV.BIAS_VALUE : GV.LINE_VALUE,
(cx + cy) * 10,
_p);
}
}
}
标记数据记录,Sign.as类
package code{
public class Sign{
private var _ix:Number;
private var _iy:Number;
private var _p:Sign;
private var _f:int = 0;
private var _g:int = 0;
private var _h:int = 0;
//f表示路径评分、g表示当前移动耗费、h表示当前估计移动耗费
//公式:f = g + h,表示路径评分的算法
//ng值表示以父标记为主标记的g值
//nh值表示当前估计移动耗费
public function Sign(_ix:Number,_iy:Number,ng:int,nh:int,_p:Sign = null){
this._ix = _ix;
this._iy = _iy;
this._p = _p;
if(_p){
_g = _p.g + ng;
_h = nh;
_f = _g + _h;
}
}
//获取该标记的坐标
public function getSign():Object{
return {x:_ix * GV.GRID_WIDTH,y:_iy * GV.GRID_HEIGHT};
}
//获取它表示的x坐标
public function get ix():int{
return _ix;
}
//获取它表示的y坐标
public function get iy():int{
return _iy;
}
//获取父标记
public function get p():Sign{
return _p;
}
//获取路径评分
public function get f():int{
return _f;
}
//获取当前路径移动耗费
public function get g():int{
return _g;
}
//获取当前路径耗费估值
public function get h():int{
return _h;
}
//重写toString值
public function toString():String{
return ix + "," + iy;
}
}
}
某点状态类,State.as
package code{
public class State{
public var value:int = 0;
public function State(){
}
//获取是否障碍
public function get isThing():Boolean{
return value == GV.HAVE_THING;
}
//获取是否死路
public function get isWalk():Boolean{
return value == GV.IMPASSE_VALUE;
}
//重写toString
public function toString():String{
return value.toString();
}
}
}
物体类:Thing.as
package code{
import flash.display.Sprite;
public class Thing extends Sprite{
public function Thing(){
init();
}
//初始化
private function init():void{
graphics.beginFill(0);
graphics.drawRect(0,0,GV.GRID_WIDTH,GV.GRID_HEIGHT);
mouseEnabled = false;
mouseChildren = false;
}
}
}
坐标系类,Coordinate.as
package code{
public dynamic class Coordinate extends Array{
public function Coordinate(){
init();
}
//初始化
private function init():void{
for(var i:int = 0; i
主角类,Main.as
package code{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
init();
}
//初始化
private function init():void{
graphics.beginFill(0x0000ff);
graphics.drawRect(0,0,GV.GRID_WIDTH,GV.GRID_HEIGHT);
mouseEnabled = false;
mouseChildren = false;
}
//获取ix坐标
public function get ix():int{
return int(x / GV.GRID_WIDTH);
}
//获取iy坐标
public function get iy():int{
return int(y / GV.GRID_HEIGHT);
}
}
}
[url=http://www.xiaos8.com/uploads/flash/seekroad.rar]
源文件打包下载[/url]
下载源文件,直接打开move.fla运行即可
本文转自:http://www.5uflash.com/flashjiaocheng/Flash-as3-jiaocheng/4791.html