function fanhui(x1, x2, y1, y2) {//计算并返回定义的坐标和对象坐标相加的平方根 Sqrt = Math.sqrt(x1+x2+y1+y2); return Sqrt; } function chushizhi(obja) { endX = random(550);//定义的X坐标 endY = random(400);//定义的X坐标 d = fanhui(endX, endY, obja._x, obja._y);//获取返回后的平方根
dx = (endX-obja._x)/d*t;//定义对象要移动的X坐标
dy = (endY-obja._y)/d*t;//定义对象要移动的Y坐标 } function tt() {//定义移动的速度 t = random(10); } function yidong(objb) {//对象移动 objb._x = objb._x+dx; objb._y = objb._y+dy;
} function movie(obj) { if (endX == undefined) {//如果定义的x坐标为空执行 tt();//运行速度函数 chushizhi(obj);//运行定义坐标函数