Flash里的A*Pathfinding

2008-04-02 11:04:59来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


  Pathfinding,A*寻路,利用这原理能够制作战棋游戏...地图内黑色块为障碍物,白色块为通道,在非寻路状态时点击能够转换.红色块为主角,点击主角再点目标可进行寻路.

  PS:寻路是四方向的,人懒就没写八方向了,FLA文档请用FLASH MX2004以上版本打开

  演示:

  

  


  以下为代码:

  


  //code by 月光 2005.9.22

  //地图长宽

  var mapx = 40;

  var mapy = 40;

  var total = mapx * mapy;

  //边界距离

  var distancex = 30;

  var distancey = 30;

  //移动速度

  var interval = 75;

  //初始障碍物比率

  var balk = 3;

  var val;

  var width = 0;

  var height = 0;

  //移动次数

  var moves = 0;

  //是否正在忙碌

  var busy = false;

  //是否正在移动

  var moving = false;

  //是否搜索成功

  var finded = false;

  //路径记录

  var open = new Array();

  //方向数组 => 右, 下, 左, 上

  var ori = new Array([1, 0], [0, 1], [-1, 0], [0, -1]);

  // ========================================

  //重置

  var reset = function () {

  clearInterval(val);

  for (var i in _root) {

   if (typeof _root[i] == "movieclip") {

   _root[i].removeMovieClip();

   }

  }

  txt.text = "";

  init();

  };

  btn_reset.onPress = reset;

  // ========================================

  //初始化

  var init = function () {

  var i = 1;

  // 绘制地图

  while (i <= total) {

   var tmp = attachMovie("mc", "m" i, i, {id:i, visited:false});

   tmp._x = (i - 1) % mapx * tmp._width distancex;

   tmp._y = Math.floor((i - 1) / mapy) * tmp._height distancey;

   if (random(balk) == 0) {

   tmp.gotoAndStop(2);

   }

   i ;

  }

  m1.gotoAndStop(1);

  width = m1._width;

  height = m1._height;

  // 主角初始位置

  attachMovie("player", "player", total 10000, {_x:distancex, _y:distancey});

  player.onPress = function() {

   busy = true;

   txt.text = "请选择目标";

  };

  moving = false;

  };

  // ========================================

  //点击地图后执行

  btn.onPress = function() {

  if (!moving) {

   var i = 1;

   while (i <= total) {

   this._parent["m" i].visited = false;

   i ;

   }

   var x = Math.ceil((_xmouse - distancex) / width);

   var y = Math.ceil((_ymouse - distancey) / height);

   var id = x (y - 1) * mapy;

   //当前点击的MC

   var tmp = this._parent["m" id];

   if (!busy) {

   if (tmp._currentframe == 1) {

   txt.text = "目标转换为障碍物";

   tmp.gotoAndStop(2);

   } else {

   txt.text = "目标转换为通道";

   tmp.gotoAndStop(1);

   }

   } else {

   if (tmp._currentframe == 2) {

   txt.text = "目标为障碍物,无法到达";

   busy = false;

   } else {

   findit(x, y);

   }

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: FLASHMX2004:平铺底纹

下一篇: FlashMX中暗藏小游戏