這樣x坐標(biāo)在0到2a之間不斷循環(huán),轉(zhuǎn)換式如下: x = (y < 0) ? 2*a-Math.abs(x) :Math.abs(x);
4.制作類
算法清楚后,把它寫成類,方便以后使用。類如下:
class Move { private var p = 2*Math.PI;//數(shù)學(xué)中的2π private var c_x, c_y, c_a, c_b, nu, time:Number; //依次為中心坐標(biāo)(c_x, c_y), 截距(c_a, c_b),nu為mc的數(shù)目,time為弧度增量 private var obj:MovieClip; //構(gòu)造函數(shù) 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);//調(diào)用復(fù)制函數(shù) } //得到和設(shè)置弧度的起始增量 public function get timer():Number { return time; } public function set timer(t:Number):Void { time = t; } //得到和設(shè)置橢圓的中心x坐標(biāo) public function get cen_x():Number { return c_x; } public function set cen_x(x:Number):Void { c_x = x; } //得到和設(shè)置橢圓的中心y坐標(biāo) public function get cen_y():Number { return c_y; } public function set cen_y(y:Number):Void { c_y = y; } //得到和設(shè)置橢圓在x坐標(biāo)軸上的截距 public function get cen_a():Number { return c_a; } public function set cen_a(a:Number):Void { c_a = a; } //得到和設(shè)置橢圓在y坐標(biāo)軸上的截距 public function get cen_b():Number { return c_b; } public function set cen_b(b:Number):Void { c_b = b; } //得到和設(shè)置橢圓上mc的數(shù)目 public function get num():Number { return nu; } public function set num(n:Number):Void { nu = n; } //設(shè)置mc的x坐標(biāo) public function set_x(radian:Number):Number { return Math.cos(radian)*c_a+c_x; } //設(shè)置mc的y坐標(biāo) public function set_y(radian:Number):Number { return Math.sin(radian)*c_b+c_y; } //復(fù)制,產(chǎn)生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跳到相應(yīng)的幀 _root["a"+j]._x = set_x(p/num*j); _root["a"+j]._y = set_y(p/num*j);//計算坐標(biāo) 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; //根據(jù)鼠標(biāo)位置確定旋轉(zhuǎn)方向,0.005為旋轉(zhuǎn)速度 } //坐標(biāo)的轉(zhuǎn)換 public function trans_x(obj:MovieClip):Number { var x:Number = obj._x-c_x; var y:Number = obj._y-c_y; //取得mc的坐標(biāo) x = (y<0) ? 2*c_a-Math.abs(x) : Math.abs(x);//轉(zhuǎn)換 return x;//返回轉(zhuǎn)換值 } //設(shè)置mc的透明度和深度 public function set_alpha(obj:MovieClip):Void { obj._alpha = trans_x(obj)/c_a*40+20;//從20到100 obj.swapDepths(trans_x(obj));//深度設(shè)置 } //設(shè)置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)); } }