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