查看: 1892|回复: 0
打印 上一主题 下一主题

[as3基础应用]AS3寻路算法-基于广度优先搜索(第二部分)

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2013-3-20 10:19:13 |只看该作者 |倒序浏览
Map.as package { import flash.display.Sprite; /** * ... * @author boycy815 */ public class Map extends Sprite { private var _nodes:Array; private var _mapWidth:int; private var _mapHeight:int; private var _root:Node; private var _target:Node; public function Map(mapWidth:int, mapHeight:int):void { _mapWidth = mapWidth; _mapHeight = mapHeight; var n:int = 0; var m:int = 0; _nodes = new Array(); for (n = 0; n < _mapHeight; n++ ) { _nodes[n] = new Array(); for (m = 0; m < _mapWidth; m++ ) { _nodes[n][m] = new Node(); _nodes[n][m].idx = m; _nodes[n][m].idy = n; _nodes[n][m].x = 20 * m; _nodes[n][m].y = 20 * n; this.addChild(_nodes[n][m]); } } for (n = 0; n < _mapHeight; n++ ) { for (m = 0; m < _mapWidth; m++ ) { if (m < _mapWidth - 1) { _nodes[n][m].childNode.push(_nodes[n][m + 1]); } if (n < _mapHeight - 1) { _nodes[n][m].childNode.push(_nodes[n + 1][m]); } if (m > 0) { _nodes[n][m].childNode.push(_nodes[n][m -1]); } if (n > 0) { _nodes[n][m].childNode.push(_nodes[n - 1][m]); } _nodes[n][m].lock = false; } } } public function lock(x:int, y:int):void { if (x<_mapWidth&&x>=0&&y<_mapHeight&&y>=0) { _nodes[y][x].locked(); _nodes[y][x].lock = ***e; if (_nodes[y][x] == _root) { _root = null; } if (_nodes[y][x] == _target) { _target = null; } } } public function unlock(x:int, y:int):void { if (x<_mapWidth&&x>=0&&y<_mapHeight&&y>=0) { _nodes[y][x].unLocked(); _nodes[y][x].lock = false; if (_nodes[y][x] == _root) { _root = null; } if (_nodes[y][x] == _target) { _target = null; } } } public function setRoot(x:int, y:int):void { if (x<_mapWidth&&x>=0&&y<_mapHeight&&y>=0) { if (_root != null) { _root.unLocked(); _root.lock = false; } _root = _nodes[y][x]; _root.theRoot(); _root.lock = ***e; } } public function setTarget(x:int, y:int):void { if (x<_mapWidth&&x>=0&&y<_mapHeight&&y>=0) { if (_target != null) { _target.unLocked(); _target.lock = false; } _target = _nodes[y][x]; _target.theTarget(); _target.lock = false; } } public function clear():void { var n:int = 0; var m:int = 0; for (n = 0; n < _mapHeight; n++ ) { for (m = 0; m < _mapWidth; m++ ) { _nodes[n][m].lock = false; _nodes[n][m].parentNode = null; unlock(m, n); } } _root=null; _target=null; } public function search():Array { var level:Array = new Array(); var tempArray:Array; var way:Array = new Array(); var goBack:Node; var n:int = 0; var m:int = 0; if (_root == null || _target == null || _root == _target) { return null; } level.push(_root); while (1) { tempArray = new Array(); for (n = 0; n < level.length; n++ ) { for (m = 0; m < level[n].childNode.length; m++ ) { if (level[n].childNode[m].lock == false) { level[n].childNode[m].lock = ***e; level[n].childNode[m].parentNode = level[n]; if (level[n].childNode[m] == _target) { goBack = level[n]; while (goBack.parentNode != null) { goBack.mark(); way.push(goBack); goBack = goBack.parentNode; } way.reverse(); return way; } else { tempArray.push(level[n].childNode[m]); } } } } if (tempArray.length == 0) { trace("无出路"); return null; } else { level = tempArray; } } return null; } }}


下一页

【来源:互联网】

更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-16 00:36 , Processed in 0.109369 second(s), 30 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部