华容道

依旧没有flash播放器……

代码:
//用数组定义棋盘格局
var aHua:Array=new Array(mCao,mGuan,mZhang,mZhao,mMa,mHuang,mBing1,mBing2,mBing3,mBing4);

var oldMouseX:Number;
var oldMouseY:Number;
var BlockDirection:String;
var theCurrentBlock:MovieClip=new MovieClip();

//设置棋子鼠标光标为手形
mCao.useHandCursor=true;
mCao.buttonMode=true;
mMa.useHandCursor=true;
mMa.buttonMode=true;
mZhao.useHandCursor=true;
mZhao.buttonMode=true;
mGuan.useHandCursor=true;
mGuan.buttonMode=true;
mHuang.useHandCursor=true;
mHuang.buttonMode=true;
mZhang.useHandCursor=true;
mZhang.buttonMode=true;
mBing1.useHandCursor=true;
mBing1.buttonMode=true;
mBing2.useHandCursor=true;
mBing2.buttonMode=true;
mBing3.useHandCursor=true;
mBing3.buttonMode=true;
mBing4.useHandCursor=true;
mBing4.buttonMode=true;

//棋子侦听鼠标落下和抬起事件,分别执行移动和停止动作
mCao.addEventListener(MouseEvent.MOUSE_DOWN,BlockMove);
mMa.addEventListener(MouseEvent.MOUSE_DOWN,BlockMove);
mZhao.addEventListener(MouseEvent.MOUSE_DOWN,BlockMove);
mZhang.addEventListener(MouseEvent.MOUSE_DOWN,BlockMove);
mGuan.addEventListener(MouseEvent.MOUSE_DOWN,BlockMove);
mHuang.addEventListener(MouseEvent.MOUSE_DOWN,BlockMove);
mBing1.addEventListener(MouseEvent.MOUSE_DOWN,BlockMove);
mBing2.addEventListener(MouseEvent.MOUSE_DOWN,BlockMove);
mBing3.addEventListener(MouseEvent.MOUSE_DOWN,BlockMove);
mBing4.addEventListener(MouseEvent.MOUSE_DOWN,BlockMove);

//棋子移动
function BlockMove(evt:MouseEvent):void {
//记录鼠标首次点击时位置
oldMouseX=mouseX;
oldMouseY=mouseY;

//获取当前被点击对象
theCurrentBlock=evt.currentTarget as MovieClip;
//当前对象的侦听事件
theCurrentBlock.addEventListener(MouseEvent.MOUSE_OUT,goAstep);

}

//走步算法
function goAstep(evt:MouseEvent):void {

//判断棋子移动方向
var dx:Number;
var dy:Number;
dx=Math.abs(mouseX-oldMouseX);
dy=Math.abs(mouseY-oldMouseY);

if (dx>dy) { //水平移动
if (mouseXoldMouseX) {
BlockDirection=”right”;
}
}
if (dx if (mouseYoldMouseY) {
BlockDirection=”down”;
}
}
if (dx==dy) { //不做移动
BlockDirection=”noway”;
}

//根据棋子移动方向走一步
switch (BlockDirection) {
case “left” :
if (theCurrentBlock.x-100>=50) { //左走一步
theCurrentBlock.x=theCurrentBlock.x-100;
}
if (! permission(theCurrentBlock)) { //有碰撞发生
theCurrentBlock.x=theCurrentBlock.x+100;
}
break;
case “right” :
if (theCurrentBlock.x+100+theCurrentBlock.width=50) { //上一步
theCurrentBlock.y=theCurrentBlock.y-100;
}
if (! permission(theCurrentBlock)) { //有碰撞发生
theCurrentBlock.y=theCurrentBlock.y+100;
}
break;
case “down” :
if (theCurrentBlock.y+100+theCurrentBlock.height theCurrentBlock.y=theCurrentBlock.y+100;
}
if (! permission(theCurrentBlock)) { //有碰撞发生
theCurrentBlock.y=theCurrentBlock.y-100;
}
break;
}
theCurrentBlock.removeEventListener(MouseEvent.MOUSE_OUT,goAstep);
}

//判断是否有碰撞发生
function permission(mObj:MovieClip):Boolean {

for (var i:int=0; iif (mObj.name!=aHua[i].name) { //不是自己
if ((mObj.hitTestPoint(aHua[i].x+5,aHua[i].y+aHua[i].height/2,false)) || (mObj.hitTestPoint(aHua[i].x+aHua[i].width-5,aHua[i].y+aHua[i].height/2,false)) || (mObj.hitTestPoint(aHua[i].x+aHua[i].width/2,aHua[i].y+5,false)) || (mObj.hitTestPoint(aHua[i].x+aHua[i].width/2,aHua[i].y+aHua[i].height-5,false))) {
return false; //不可以动
}
}
}
return true; //可以动
}

作者 hsyyf

在 “华容道” 有 1 条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注