1. package
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.DisplayObject;
  6. import flash.display.Sprite;
  7. import flash.events.MouseEvent;
  8. import flash.events.TimerEvent;
  9. import flash.geom.PerspectiveProjection;
  10. import flash.geom.Point;
  11. import flash.geom.Vector3D;
  12. import flash.utils.Timer;
  13. import flash.utils.getDefinitionByName;
  14.  
  15. /**
  16. * @author Frost.Yen
  17. * @E-mail 871979853@qq.com
  18. * @create 2015-8-13 下午4:03:27
  19. *
  20. */
  21. [SWF(width="1024",height="768")]
  22. public class RotateMain extends Sprite
  23. {
  24. [Embed(source="assets/001.png")]
  25. private var _img0:Class;
  26. [Embed(source="assets/002.png")]
  27. private var _img1:Class;
  28. [Embed(source="assets/003.png")]
  29. private var _img2:Class;
  30. [Embed(source="assets/004.png")]
  31. private var _img3:Class;
  32. [Embed(source="assets/005.png")]
  33. private var _img4:Class;
  34. [Embed(source="assets/006.png")]
  35. private var _img5:Class;
  36. [Embed(source="assets/007.png")]
  37. private var _img6:Class;
  38. [Embed(source="assets/007.png")]
  39. private var _img7:Class;
  40.  
  41. private var _num:int = 7;
  42. private var _radius:Number = 300;
  43. private var _itemArr:Array = [];
  44. private var _speed:Number = 1;
  45. private var _ratio:Number = 0.25;//系数
  46. private var _downX:Number;
  47. private var _container:Sprite;
  48. private var _timer:Timer = new Timer(20);
  49. public function RotateMain()
  50. {
  51. this.graphics.beginFill(0,1);
  52. this.graphics.drawRect(0,0,1024,768);
  53. this.graphics.endFill();
  54. initViews();
  55. initEventListeners();
  56. }
  57. private function initViews():void
  58. {
  59. _container = new Sprite();
  60. _container.x = 1024*0.5;
  61. _container.y = 768*0.5-200;
  62. _container.z = 0;
  63. this.addChild(_container);
  64. for(var i:int = 0;i<_num;i++){
  65. var angle:Number = Math.PI * 2/_num*i;
  66. var sp:Sprite = new Sprite();
  67. //var img:Class = getDefinitionByName("RotateMain__img"+i) as Class;
  68. var bmp:Bitmap = new this["_img"+i]();
  69.  
  70. sp.x = Math.cos(angle)*_radius;
  71. sp.z = Math.sin(angle)*_radius;
  72. sp.addChild(bmp);
  73. _container.addChild(sp);
  74. _itemArr.push(sp);
  75. }
  76. swapItems();
  77. _timer.addEventListener(TimerEvent.TIMER,onTimer);
  78. _timer.start();
  79.  
  80. }
  81. private function initEventListeners():void
  82. {
  83. stage.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
  84. }
  85. private function onDown(e:MouseEvent):void
  86. {
  87. stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
  88. stage.addEventListener(MouseEvent.MOUSE_UP,onUp);
  89. _timer.stop();
  90. _downX = mouseX;
  91. }
  92. private function onMove(e:MouseEvent):void
  93. {
  94. _speed = (_downX-mouseX)*0.01;
  95. _container.rotationY += _ratio * _speed;
  96. var p:PerspectiveProjection = new PerspectiveProjection();
  97. p.fieldOfView = 55;
  98. p.projectionCenter = new Point(512,-100);
  99. _container.transform.perspectiveProjection = p;
  100. swapItems();
  101.  
  102. }
  103. private function onUp(e:MouseEvent):void
  104. {
  105. stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
  106. stage.removeEventListener(MouseEvent.MOUSE_UP,onUp);
  107. _timer.start();
  108. }
  109. private function onTimer(e:TimerEvent):void
  110. {
  111. if(_speed < -1){
  112. _speed+=0.02;
  113. if(_speed >=-1){
  114. _speed = -1;
  115. }
  116. }else if(_speed >1){
  117. _speed-=0.02;
  118. if(_speed <=1){
  119. _speed = 1;
  120. }
  121. }else if(_speed>-1&&_speed<0){
  122. _speed-=0.02;
  123. }else if(_speed<1&&_speed>0){
  124. _speed+=0.02;
  125. }
  126. _container.rotationY += _ratio *_speed ;//trace(_ratio,_speed);
  127. var p:PerspectiveProjection = new PerspectiveProjection();
  128. p.fieldOfView = 55;
  129. p.projectionCenter = new Point(512,-100) ; //视点
  130. _container.transform.perspectiveProjection = p;
  131. swapItems();
  132. }
  133. private function swapItems():void
  134. {
  135. _itemArr.sort(swapDepth);
  136. for(var i:int = 0; i < _itemArr.length; i++ )
  137. {
  138. _container.addChildAt(_itemArr[i] as Sprite,i);
  139. (_itemArr[i] as Sprite).rotationY -= _ratio *_speed;
  140. }
  141. }
  142. private function swapDepth(objA:DisplayObject,objB:DisplayObject):int
  143. {
  144. var posA:Vector3D = objA.transform.matrix3D.position ;
  145. posA = _container.transform.matrix3D.deltaTransformVector(posA);
  146. var posB:Vector3D = objB.transform.matrix3D.position;
  147. posB = _container.transform.matrix3D.deltaTransformVector(posB);
  148. return posB.z - posA.z;
  149. }
  150. }
  151. }

[ActionScript 3.0] AS3实现3D旋转的更多相关文章

  1. [ActionScript 3.0] as3处理xml的功能和遍历节点

    as3比as2处理xml的功能增强了N倍,获取或遍历节点非常之方便,类似于json对像的处理方式. XML 的一个强大功能是它能够通过文本字符的线性字符串提供复杂的嵌套数据.将数据加载到 XML 对象 ...

  2. [ActionScript 3.0] AS3.0 动态加载显示内容

    可以将下列任何外部显示资源加载到 ActionScript 3.0 应用程序中: 在 ActionScript 3.0 中创作的 SWF 文件 — 此文件可以是 Sprite.MovieClip 或扩 ...

  3. [ActionScript 3.0] AS3 3D双圆环贴图

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.MovieCl ...

  4. [ActionScript 3.0] AS3 3D星形贴图

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.MovieCl ...

  5. [ActionScript 3.0] AS3.0和AS2.0的相互通信

    AS3和AS2之间的通信,最好的方式可能就是LocalConnection了. AS2向AS3发送数据,即AS2调用AS3的函数: as2.0代码(按钮上写的发送信息代码): on (release) ...

  6. [ActionScript 3.0] AS3.0 本机鼠标指针

    Flash Player 10.2添加了内置的本机鼠标指针(native mouse cursor)支持,虽然在之前的版本里我们可以侦听MouseEvent事件来模拟鼠标指针,但是在有了原生的本机鼠标 ...

  7. [ActionScript 3.0] AS3 深入理解Flash的安全沙箱Security Domains

    简介 如果你还没有与复杂的的安全域(security domain)和应用程序域(application domain)问题打过交道,那么你真是个幸运的家伙.当你在加载外部内容(然后他们开始播放)的时 ...

  8. [ActionScript 3.0] AS3 深入理解Flash的 应用程序域Application Domains

    简介 网上有很多flash,通常都不需要显示的使用应用程序域,因为默认的应用程序域就够用了.其实复杂的情况下需要用到应用程序域,比如说有两个不同的swf,一个是旧版本的,一个是新版的,这两个文件里的类 ...

  9. [ActionScript 3.0] AS3调用百度天气预报查询API

    接口说明 根据经纬度/城市名查询天气的结果 接口示例 http://api.map.baidu.com/telematics/v3/weather?location=成都&output=jso ...

随机推荐

  1. html学习代码

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  2. 【sdut2878】Circle

    题目链接http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2878.html 题意 n个结点编号为0 ...

  3. [hdu2665]Kth number(划分树求区间第k大)

    解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...

  4. Zedboard学习(三):PL下流水灯实验 标签: fpgazynqPL 2017-07-05 11:09 21人阅读 评论(0)

    zynq系列FPGA分为PS部分和PL部分. PL: 可编程逻辑 (Progarmmable Logic), 就是FPGA部分. PS: 处理系统 (Processing System) , 就是与F ...

  5. [Training Video - 4] [Groovy] Function in detail

    Employee.log=log Employee e1 = new Employee() Employee e2 = new Employee() e1.name = "A" e ...

  6. [BAT] 通过批处理删除7天前的报告,并删除当前目录下的空文件夹

    set reportPath=D:\AutomationReport cd /d %reportPath% forfiles /p %reportPath% /s /m *.xml /d -7 /c ...

  7. datatable:dt.page(dt.page()).draw(false)

    dt.page(dt.page()).draw(false);该方法可以直接返回到当前页,不用重新绘制table 描述 分页是DataTables的一个核心功能,并且该方法提供对表格显示页面的外部控制 ...

  8. WindowServer2016无法安装.netframework3.5

    因为安装sql server的原因 需要安装.NET Framework3.5 报错内容如下: 原因分析 找不到安装源文件. 解决办法 可以通过如下 PowerShell 脚本进行安装: 从开始菜单中 ...

  9. (转)TinyHttp源码剖析

    tinyhttpd 是一个不到 500 行的超轻量型 Http Server,用来学习非常不错,可以帮助我们真正理解服务器程序的本质. 看完所有源码,真的感觉有很大收获,无论是 unix 的编程,还是 ...

  10. 开源SLAM

    GitHub 上优秀的开源SLAM repo (更新中):https://www.jianshu.com/p/464ca0d0c254 当前的开源SLAM方案:https://www.cnblogs. ...