首先,先在CS4里,建立一个MC,加入三个按钮,如图
我暂时把地形全都设定成为可移动,且地形全都消耗1,1S.xml中
<DataMap>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
<list>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</list>
</DataMap>
加入鼠标点击事件
//鼠标点击事件
addEventListener(MouseEvent.CLICK,onClick);
//鼠标点击事件
private function onClick(event:Event):void{
var __mouseX:Number = event.currentTarget.mouseX;
var __mouseY:Number = event.currentTarget.mouseY;
var i:int;
if(_clickCtrl == "NULL"){
for(i=0;i<_ourArr.length;i++){
_character = _ourArr as Character;
_characterMC = _character.characterMC;
if(Math.floor(_characterMC.x/48) == Math.floor(__mouseX/48) && Math.floor(_characterMC.y/48) == Math.floor(__mouseY/48)){
_nowChatacter = _character;
setRoad(_nowChatacter);
_clickCtrl = "ROAD_SHOW";
removeEventListener(MouseEvent.CLICK,onClick);
_roadSprite.addEventListener(MouseEvent.CLICK,onClick);
break;
}
}
}else if(_clickCtrl == "ROAD_SHOW"){
_characterMC = _nowChatacter.characterMC;
for(i=0;i<_roadShowArray.length;i++){
_nowPoint = new Point(Math.floor(_characterMC.x/48),Math.floor(_characterMC.y/48));
_nowDirection = _characterMC.getDir();
_toPoint = new Point(Math.floor(__mouseX/48),Math.floor(__mouseY/48));
_roadArray = _roadQuery.path4(_nowPoint,_toPoint,_roadShowArray);
if(_roadArray != null && _roadArray.length > 0){
addEventListener(Event.ENTER_FRAME, onFrame);
_clickCtrl == "MOVE_NOW"
SpriteRemove.removeAllChildren(_roadSprite);
_roadSprite.removeEventListener(MouseEvent.CLICK,onClick);
}
}
}
}
_clickCtrl 用来控制点击鼠标时,战场上的状态,当为NULL的时候,寻找点击的人物,然后开始寻找最大可移动的范围
//循环搜寻可到达的点
public function loopPath(thisPoint:Object):void{
if(thisPoint.moveLong <= 0){
return;
}
if(! thisPoint.isChecked){
_path.push(thisPoint);
thisPoint.isChecked = true;
}
var checkList:Array = [];
//获取周围四个点
if (thisPoint.y>0) {
checkList.push(_map[(thisPoint.y-1)][thisPoint.x]);
}
if (thisPoint.x>0) {
checkList.push(_map[thisPoint.y][(thisPoint.x-1)]);
}
if (thisPoint.x<_w-1) {
checkList.push(_map[thisPoint.y][(thisPoint.x+1)]);
}
if (thisPoint.y<_h-1) {
checkList.push(_map[(thisPoint.y+1)][thisPoint.x]);
}
for(var i=0;i<checkList.length;i++){
if(!checkList.isChecked || checkList.moveLong < thisPoint.moveLong){
checkList.moveLong = thisPoint.moveLong - 1;
loopPath(checkList);
}
}
}
//搜寻最大路径
public function makePath(star:Point, moveLong:int):Array {
_path = [];
var isOver:Boolean = false;
setStart();
_starPoint = _map[star.y][star.x];
_starPoint.moveLong = moveLong;
loopPath(_starPoint);
return _path;
}
最大路径找好后,点击目标点,利用A*算法,走到目标点
_clickCtrl 用来控制点击鼠标时,战场上的状态,当为ROAD_SHOW的时候,查找路径,并走到目标点
A*算法,比较麻烦,就不多解释了,想了解的,可以看源代码
然后,行走完毕后,要显示出选择菜单,
//人物移动事件贞控制
private function onFrame(event:Event):void {
_characterMC = _nowChatacter.characterMC;
if(_roadArray != null && _roadArray.length > 1){
if(_characterMC.x % 48 == 0 && _characterMC.y % 48 == 0){
if(_roadArray[0].x == _roadArray[1].x){
if(_roadArray[0].y > _roadArray[1].y){
_characterMC.setDir(0);
_xadd = 0;
_yadd = -_addLeng;
}else{
_characterMC.setDir(1);
_xadd = 0;
_yadd = _addLeng;
}
}else{
if(_roadArray[0].x > _roadArray[1].x){
_characterMC.setDir(2);
_xadd = -_addLeng;
_yadd = 0;
}else{
_characterMC.setDir(3);
_xadd = _addLeng;
_yadd = 0;
}
}
_roadArray.splice(0,1);
}
_characterMC.x += _xadd;
_characterMC.y += _yadd;
}else if(_characterMC.x % 48 == 0 && _characterMC.y % 48 == 0){
removeEventListener(Event.ENTER_FRAME, onFrame);
_selectBox.visible = true;
_selectBox.x = _characterMC.x + 48;
_selectBox.y = _characterMC.y;
}else{
_characterMC.x += _xadd;
_characterMC.y += _yadd;
}
}
所以在战场初始化的时候,加入菜单按钮等事件
//点击停止事件
_selectBox.btnStop.addEventListener(MouseEvent.CLICK, charaStopHandler);
//点击取消事件
_selectBox.btnCan.addEventListener(MouseEvent.CLICK, charaCanHandler);
//菜单隐藏
_selectBox.visible = false;
效果如下图
[ 本帖最后由 lufy 于 2010-5-19 22:54 编辑 ]
|