A星寻路算法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//主要类
package
{
import classes.AStar;
import classes.Grid;
import classes.Node;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.PerspectiveProjection;
import flash.utils.getTimer;
import Util;
import yout.PaddingLayoutFacet;
/**
*
* @author LB
*
*/
public class AStarPathFind extends Sprite
{
private var i:int;
private var j:int;
private var grid:Grid;
private var cellWidth:int;
public function AStarPathFind()
{
initMap();
initPlayer();
}
private var player:MovieClip;
private function initPlayer():void
{
player = new MovieClip();
player.graphics.beginFill(0x000000);
player.graphics.drawRect(0,0,Node.cellWidth,Node.cellWidth);
player.graphics.endFill();
this.addChild(player);
var rx:int = int(Math.random()*grid._col);
var ry:int = int(Math.random()*grid._row);
grid.setStartNode(rx,ry);
var test:Node = grid.startNode;
trace("=====",test.x,test.y);
player.x = rx*Node.cellWidth;
player.y = ry*Node.cellWidth;
stage.addEventListener(MouseEvent.CLICK,prepareFindPath);
}
private var pen:Shape = new Shape();
private function drawPath(path:Array):void
{
trace("=++++++======",path);
for(i=1;i { var node:Node = path[i] as Node; pen.graphics.beginFill(0xcc00aa); pen.graphics.drawRect(node.x*Node.cellWidth,node.y*Node.cellWidth,Node.cellWidth,Nod e.cellWidth); pen.graphics.endFill(); this.addChild(pen); } } private var currentMouseXY:Array=[];//存储当前鼠标点南的坐标 /** * 点击舞台准备寻路 * **/ private function prepareFindPath(event:MouseEvent):void { var clickX:int = Math.floor(this.mouseX/Node.cellWidth);//当前鼠标点击的位置 var clickY:int = Math.floor(this.mouseY/Node.cellWidth); if(clickX<0||clickY<0) return;//点击在外面了 var clickNode:Node = grid.getNode(clickX,clickY); if(!clickNode) return;//点击在外面了 if(!clickNode.isWalk) trace("==目标是障碍物==="); return; } if(currentMouseXY.length!=0) { if(currentMouseXY[0]==clickX&¤tMouseXY[1]==clickY) { trace("点击是同一位置"); return; } else { currentMouseXY[0] = clickX; currentMouseXY[1] = clickY; grid.setStartNode(int(player.x/Node.cellWidth),int(player.y/Node.cellWidth)); grid.setEndNode(clickX,clickY); } } else//第一次寻路 { currentMouseXY[0] = clickX; currentMouseXY[1] = clickY; grid.setEndNode(clickX,clickY); } pen.graphics.clear(); pen.graphics.lineStyle(1); pen.graphics.beginFill(0x000000); pen.graphics.drawRect(clickX*cellWidth,clickY*cellWidth,cellWidth,cellWidth); pen.graphics.endFill(); a = new AStar(); if(a.findPath(grid)) { index = 0; trace(a.pathArr); trace("===time===",getTimer()); // drawPath(a.pathArr); if(!player.hasEventListener(Event.ENTER_FRAME)) { player.addEventListener(Event.ENTER_FRAME,roleMoveHandler); } }