- 最后登录
- 2018-12-19
- 注册时间
- 2012-8-20
- 阅读权限
- 90
- 积分
- 54706
- 纳金币
- 32328
- 精华
- 41
|
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/ |
|