ActionScript实现长方形旋转

package 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.events.TimerEvent;

	public class Main extends Sprite
	{
		private var rect_mc:MovieClip = null;
		private var circle_mc:MovieClip = null;
		private var array1:Array=new Array(
		{m_x:275,m_y:100},{m_x:325,m_y:113.4},{m_x:361.6,m_y:150}
		,{m_x:375,m_y:200},{m_x:361.6,m_y:250},{m_x:325,m_y:286.6}
		,{m_x:275,m_y:300},{m_x:225,m_y:286.6},{m_x:188.4,m_y:250}
		,{m_x:175,m_y:200},{m_x:188.4,m_y:150},{m_x:225,m_y:113.4});
		private var counter:Number = 0;
		public function Main()
		{
			/*圆,圆画在元件上默认圆心坐标(0,0)
			也就是说圆心和元件中心重合*/
			circle_mc=new MovieClip();
			var circle_s:Sprite=new Sprite();
			with (circle_s)
			{
				graphics.lineStyle(5,0x000000);
				graphics.drawCircle(0,0,100);
			}
			circle_mc.addChild(circle_s);
			circle_mc.x = stage.stageWidth / 2;
			circle_mc.y = stage.stageHeight / 2;
			stage.addChild(circle_mc);
			/*长方形,长方形画在元件上默认左上角坐标(0,0)
			也就是说长方形左上角和元件中心重合*/
			rect_mc=new MovieClip();
			var rect_s:Sprite=new Sprite();
			with (rect_s)
			{
				graphics.lineStyle(2,0x000000);
				graphics.beginFill(0x000000);
				graphics.drawRect(0,0,10,50);
				graphics.endFill();
			}
			/*将长方形的“下边中心”和元件的中心重合
			这样在旋转长方形的时候就会以“下边中心”为基点*/
			rect_s.x = -5;
			rect_s.y = -50;
			rect_mc.addChild(rect_s);
			rect_mc.x = stage.stageWidth / 2;
			rect_mc.y = stage.stageHeight / 2;
			stage.addChild(rect_mc);
			var timer:Timer = new Timer(1000,12);
			timer.addEventListener(TimerEvent.TIMER,rectangleRotating);
			timer.start();
		}
		//长方形旋转
		private function rectangleRotating(e:TimerEvent):void
		{
			var angle1:Number = getAngleBy2Points(rect_mc.x,rect_mc.y,array1[counter].m_x,array1[counter].m_y);
			trace(angle1);
			rect_mc.rotation = angle1;
			counter++;
		}
		/*获取长方形元件的中心点与鼠标点击点连线和长方形的角度
		x1代表中心点的X坐标
		y1代表中心点的Y坐标
		x2代表鼠标点的X坐标
		y2代表鼠标垫的Y坐标*/
		private function getAngleBy2Points(x1:Number,y1:Number,x2:Number,y2:Number):Number
		{
			//获取长方形元件的中心点与鼠标点击点连线和长方形的弧度
			var radian1:Number=Math.atan((x2-x1)/(y1-y2));
			//根据弧度获取角度
			var angle1:Number=radian1/(Math.PI/180);
			if (x2 >= x1)
			{
				if (y2 <= y1)
				{
					angle1 = angle1;
				}
				else
				{
					angle1 +=  180;
				}
			}
			else
			{
				if (y2 > y1)
				{
					angle1 +=  180;
				}
				else
				{
					angle1 +=  360;
				}
			}
			return angle1;
		}
	}
}

你可能感兴趣的:(旋转,actionscript,长方形)