·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3实现长方形的转动效果源代码实例

时间:2014-07-14 10:23酷播
[AS3]as3实现长方形的转动效果源代码实例

[AS3]as3实现长方形的转动效果源代码实例

  1. package  
  2.     import flash.display.Sprite; 
  3.     import flash.events.MouseEvent; 
  4.     import flash.display.MovieClip; 
  5.     import flash.events.Event; 
  6.     import flash.utils.Timer; 
  7.     import flash.events.TimerEvent; 
  8.  
  9.     public class Main extends Sprite 
  10.     { 
  11.         private var rect_mc:MovieClip = null
  12.         private var circle_mc:MovieClip = null
  13.         private var array1:Array=new Array( 
  14.         {m_x:275,m_y:100},{m_x:325,m_y:113.4},{m_x:361.6,m_y:150} 
  15.         ,{m_x:375,m_y:200},{m_x:361.6,m_y:250},{m_x:325,m_y:286.6} 
  16.         ,{m_x:275,m_y:300},{m_x:225,m_y:286.6},{m_x:188.4,m_y:250} 
  17.         ,{m_x:175,m_y:200},{m_x:188.4,m_y:150},{m_x:225,m_y:113.4}); 
  18.         private var counter:Number = 0
  19.         public function Main() 
  20.         { 
  21.             /*圆,圆画在元件上默认圆心坐标(0,0) 
  22.             也就是说圆心和元件中心重合*/ 
  23.             circle_mc=new MovieClip(); 
  24.             var circle_s:Sprite=new Sprite(); 
  25.             with (circle_s) 
  26.             { 
  27.                 graphics.lineStyle(5,0x000000); 
  28.                 graphics.drawCircle(0,0,100); 
  29.             } 
  30.             circle_mc.addChild(circle_s); 
  31.             circle_mc.x = stage.stageWidth / 2; 
  32.             circle_mc.y = stage.stageHeight / 2; 
  33.             stage.addChild(circle_mc); 
  34.             /*长方形,长方形画在元件上默认左上角坐标(0,0) 
  35.             也就是说长方形左上角和元件中心重合*/ 
  36.             rect_mc=new MovieClip(); 
  37.             var rect_s:Sprite=new Sprite(); 
  38.             with (rect_s) 
  39.             { 
  40.                 graphics.lineStyle(2,0x000000); 
  41.                 graphics.beginFill(0x000000); 
  42.                 graphics.drawRect(0,0,10,50); 
  43.                 graphics.endFill(); 
  44.             } 
  45.             /*将长方形的“下边中心”和元件的中心重合 
  46.             这样在旋转长方形的时候就会以“下边中心”为基点*/ 
  47.             rect_s.x = -5; 
  48.             rect_s.y = -50; 
  49.             rect_mc.addChild(rect_s); 
  50.             rect_mc.x = stage.stageWidth / 2; 
  51.             rect_mc.y = stage.stageHeight / 2; 
  52.             stage.addChild(rect_mc); 
  53.             var timer:Timer = new Timer(1000,12); 
  54.             timer.addEventListener(TimerEvent.TIMER,rectangleRotating); 
  55.             timer.start(); 
  56.         } 
  57.         //长方形旋转 
  58.         private function rectangleRotating(e:TimerEvent):void 
  59.         { 
  60.             var angle1:Number = getAngleBy2Points(rect_mc.x,rect_mc.y,array1[counter].m_x,array1[counter].m_y); 
  61.             trace(angle1); 
  62.             rect_mc.rotation = angle1
  63.             counter++; 
  64.         } 
  65.         /*获取长方形元件的中心点与鼠标点击点连线和长方形的角度 
  66.         x1代表中心点的X坐标 
  67.         y1代表中心点的Y坐标 
  68.         x2代表鼠标点的X坐标 
  69.         y2代表鼠标垫的Y坐标*/ 
  70.         private function getAngleBy2Points(x1:Number,y1:Number,x2:Number,y2:Number):Number 
  71.         { 
  72.             //获取长方形元件的中心点与鼠标点击点连线和长方形的弧度 
  73.             var radian1:Number=Math.atan((x2-x1)/(y1-y2)); 
  74.             //根据弧度获取角度 
  75.             var angle1:Number=radian1/(Math.PI/180); 
  76.             if (x2 >= x1) 
  77.             { 
  78.                 if (y2 <= y1) 
  79.                 { 
  80.                     angle1angle1 = angle1; 
  81.                 } 
  82.                 else 
  83.                 { 
  84.                     angle1 +=  180; 
  85.                 } 
  86.             } 
  87.             else 
  88.             { 
  89.                 if (y2 > y1) 
  90.                 { 
  91.                     angle1 +=  180; 
  92.                 } 
  93.                 else 
  94.                 { 
  95.                     angle1 +=  360; 
  96.                 } 
  97.             } 
  98.             return angle1; 
  99.         } 
  100.     } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器