两个物体通过弧线运动互换位置
swapPosition(a,b,5,-50);
//横坐标比较大的对象运动轨迹(向着坐标比较小的对象处移动)
function movingByArc(object:MovieClip,disX:Number,k:Number,isPositionLarger:Boolean,speed:Number)
{
var i:Number = 0;
var x0:Number = object.x;
var y0:Number = object.y;
object.addEventListener(Event.ENTER_FRAME,onEventFunction);
//开始沿着sin线运动
function onEventFunction(e:Event):void
{
//如果互相交换到位,停止运动
if (Math.abs(i) == 180)
{
object.removeEventListener(Event.ENTER_FRAME,onEventFunction);
}
var xx = x0 + i / disX;
var yy=y0-Math.sin(i*Math.PI/180)*k;
object.x = xx;
object.y = yy;
//坐标大小不同运动轨迹就不同
if (isPositionLarger)
{
i -= speed;
}
else
{
i += speed;
}
}
}
function swapPosition(ob1:MovieClip,ob2:MovieClip,speed:Number,k:Number):void
{
//var k:Number = 50;//y轴幅度大小
var disX=180/Math.abs((ob1.x-ob2.x));//x轴大小幅度
if (ob1.x > ob2.x)
{
movingByArc(ob1,disX,k,true,speed);
movingByArc(ob2,disX,k,false,speed);
}
else
{
movingByArc(ob1,disX,k,false,speed);
movingByArc(ob2,disX,k,true,speed);
}
}