这样x坐标在0到2a之间不断循环,转变式如下: x = (y < 0) ? 2*a-Math.abs(x) :Math.abs(x);
4.制作类
算法清晰后,把它写成类,方便以后使用。类如下:
class Move { private var p = 2*Math.PI;//数学中的2π private var c_x, c_y, c_a, c_b, nu, time:Number; //依次为中央坐标(c_x, c_y), 截距(c_a, c_b),nu为mc的数目,time为弧度增量 private var obj:MovieClip; //构造函数 public function Move(x, y, a, b, n, t:Number, o:MovieClip) { c_x = x; c_y = y; c_a = a; c_b = b; nu = n; time = t; duplicate(o);//调用复制函数 } //得到和设置弧度的起始增量 public function get timer():Number { return time; } public function set timer(t:Number):Void { time = t; } //得到和设置椭圆的中央x坐标 public function get cen_x():Number { return c_x; } public function set cen_x(x:Number):Void { c_x = x; } //得到和设置椭圆的中央y坐标 public function get cen_y():Number { return c_y; } public function set cen_y(y:Number):Void { c_y = y; } //得到和设置椭圆在x坐标轴上的截距 public function get cen_a():Number { return c_a; } public function set cen_a(a:Number):Void { c_a = a; } //得到和设置椭圆在y坐标轴上的截距 public function get cen_b():Number { return c_b; } public function set cen_b(b:Number):Void { c_b = b; } //得到和设置椭圆上mc的数目 public function get num():Number { return nu; } public function set num(n:Number):Void { nu = n; } //设置mc的x坐标 public function set_x(radian:Number):Number { return Math.cos(radian)*c_a+c_x; } //设置mc的y坐标 public function set_y(radian:Number):Number { return Math.sin(radian)*c_b+c_y; } //复制,产生num个mc,并均匀分布 public function duplicate(obj:MovieClip) { var j:Number = 1; while (j<=num) { obj.duplicateMovieClip("a"+j, j); _root["a"+j].n = p/num*j; //每个mc的起始弧度值 _root["a"+j].gotoAndStop(j);//mc跳到相应的帧 _root["a"+j]._x = set_x(p/num*j); _root["a"+j]._y = set_y(p/num*j);//计算坐标 j++; } } //mc的运动 public function myMove(obj:MovieClip):Void { obj._x = set_x(obj.n+time); obj._y = set_y(obj.n+time);//每个mc弧度等于起始弧度值加增量 time time = _xmouse>c_x ? time+0.005 : time-0.005; //根据鼠标位置确定旋转方向,0.005为旋转速度 } //坐标的转变 public function trans_x(obj:MovieClip):Number { var x:Number = obj._x-c_x; var y:Number = obj._y-c_y; //取得mc的坐标 x = (y<0) ? 2*c_a-Math.abs(x) : Math.abs(x);//转变 return x;//返回转变值 } //设置mc的透明度和深度 public function set_alpha(obj:MovieClip):Void { obj._alpha = trans_x(obj)/c_a*40+20;//从20到100 obj.swapDepths(trans_x(obj));//深度设置 } //设置mc的x方向的缩放 public function set_xscale(obj:MovieClip):Void { obj._xscale = trans_x(obj)/c_a*100-100;//从-100到100 obj.swapDepths(trans_x(obj)); } }