[ActionScript 3.0] AS3实现3D旋转

  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 [email protected]
 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 }
时间: 2024-10-16 05:53:11

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

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

as3比as2处理xml的功能增强了N倍,获取或遍历节点非常之方便,类似于json对像的处理方式. XML 的一个强大功能是它能够通过文本字符的线性字符串提供复杂的嵌套数据.将数据加载到 XML 对象时,ActionScript 会分析数据并将其分层结构加载到内存(如果 XML 数据格式有误,它会发送运行时错误). 利用 XML 和 XMLList 对象的运算符和方法可以轻松遍历 XML 数据的结构. 1.读取外部 XML 文档 可以使用 URLLoader 类从 URL 加载 XML 数据.若

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

1 package 2 { 3 import flash.display.Bitmap; 4 import flash.display.BitmapData; 5 import flash.display.MovieClip; 6 import flash.display.Sprite; 7 import flash.display.TriangleCulling; 8 import flash.events.Event; 9 import flash.geom.ColorTransform;

[ActionScript 3.0] AS3 3D星形贴图

1 package 2 { 3 import flash.display.Bitmap; 4 import flash.display.BitmapData; 5 import flash.display.MovieClip; 6 import flash.display.Sprite; 7 import flash.display.TriangleCulling; 8 import flash.events.Event; 9 import flash.geom.Vector3D; 10 11

[ActionScript 3.0] AS3 绘制正八面体(线条)

分析: 将八面体置于3D坐标系中,其中心的坐标位于原点(0,0,0),让八面体的六个顶点恰好位于3D坐标系的x轴.y轴和z轴上,则从八面体的中心到这六个顶点的距离是相等的.我们可以假设这个距离为r,则六个顶点的坐标分别为上(0,-r,0).下(0,r,0).四周:左(-r,0,0).右(r,0,0).前(0,0,-r).后(0,0,r). 1 package 2 { 3 import flash.display.MovieClip; 4 import flash.display.Sprite;

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

简介 如果你还没有与复杂的的安全域(security domain)和应用程序域(application domain)问题打过交道,那么你真是个幸运的家伙.当你在加载外部内容(然后他们开始播放)的时候,默认的设置工作的很好,你甚至不知道他们的存在. 但是某些时候你可能需要控制默认设置以外的更多行为和功能,这样你就会遇到前面所说的问题.你也许会困扰于Security.allowDomain和crossdomain.xml文件的区别,又或者你想要深究关于安全性的最佳实践.如果是这样,那么这篇文章就

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

AS3和AS2之间的通信,最好的方式可能就是LocalConnection了. AS2向AS3发送数据,即AS2调用AS3的函数: as2.0代码(按钮上写的发送信息代码): on (release) { var param = "this message is from as2"; var caller:LocalConnection = new LocalConnection(); caller.send("AS2 send to AS3","funI

[ActionScript 3.0] AS3虚线绘制方法

import flash.geom.Point; import flash.display.MovieClip; import flash.display.Graphics; var mc:MovieClip=new MovieClip(); addChild(mc); drawDashed(mc,new Point(100,100),new Point(500,100),5,5); function drawDashed(mc:Sprite,p1:Point,p2:Point,length:N

[ActionScript 3.0] AS3 对XML的操作,创建、删除、增加节点方法

package { import flash.display.Sprite; /** * @author:Frost.Yen * @E-mail:[email protected] * @create: 2016-3-11 上午11:31:11 * */ public class OperateXML extends Sprite { private var _xml:XML = <data> <item>item1</item> <item>item2&l

[ActionScript 3.0] AS3.0 对象在一定范围随机显示不重叠

import flash.geom.Rectangle; import flash.display.MovieClip; import flash.display.Sprite; var arr:Array = []; var dis:Number = 20;//间距 var len:int=15;//对象数量 var bound:Rectangle = new Rectangle(0,0,1000,800);//显示范围 for(var i:int = 0;i<len;i++){ var ob