1 package 2 { 3 import flash.display.Sprite; 4 import flash.events.Event; 5 import flash.geom.Vector3D; 6 import flash.text.TextField; 7 import flash.text.TextFormat; 8 9 /** 10 * @author Frost.Yen 11 * @E-mail 871979853@qq.com 12 * @create 2015-9-2 下午3:17:09 13 * 14 */ 15 [SWF(width="800",height="600")] 16 public class DrawCube extends Sprite 17 { 18 private var _faceArr:Array = []; 19 private var _sideLen:Number = 100;//正方形边长 20 private var _container:Sprite; 21 private var _text:String = "AS世界你好"; 22 private var _tf:TextFormat = new TextFormat("微软雅黑",60,0xff0000); 23 public function DrawCube() 24 { 25 initViews(); 26 initEventListeners(); 27 } 28 private function initViews():void 29 { 30 _container = new Sprite(); 31 _container.x = 300; 32 _container.y = 300; 33 this.addChild(_container); 34 creatCube(); 35 } 36 private function initEventListeners():void 37 { 38 this.addEventListener(Event.ENTER_FRAME,onMove); 39 } 40 private function onMove(e:Event):void 41 { 42 for(var i:int = 0;i<6;i++){ 43 _faceArr[i].transform.matrix3D.appendRotation((mouseX-_container.x)/50,Vector3D.Y_AXIS); 44 _faceArr[i].transform.matrix3D.appendRotation((_container.y-mouseY)/50,Vector3D.X_AXIS); 45 } 46 _faceArr.sortOn("z",18); 47 for(i=0;i<_faceArr.length;i++){ 48 _container.setChildIndex(_faceArr[i],i); 49 } 50 } 51 private function creatCube():void 52 { 53 for(var i:int=0;i<6;i++){ 54 var rect:Sprite = drawRect(_sideLen,_sideLen,Math.random()*0xffffff); 55 var txt:TextField = new TextField(); 56 _container.addChild(rect); 57 rect.addChild(txt); 58 txt.autoSize = "center"; 59 txt.selectable = false; 60 txt.defaultTextFormat = _tf; 61 txt.text = _text.substr(i,1); 62 txt.x = -txt.width*0.5; 63 txt.y = -txt.height*0.5; 64 rect.z = -_sideLen/2; 65 _faceArr[i] = rect; 66 i<4?rect.transform.matrix3D.appendRotation(90*i,Vector3D.Y_AXIS):0; 67 } 68 _faceArr[4].transform.matrix3D.appendRotation(-90,Vector3D.X_AXIS); 69 _faceArr[5].transform.matrix3D.appendRotation(90,Vector3D.X_AXIS); 70 } 71 private function drawRect(Number,height:Number,color:uint):Sprite 72 { 73 var rect:Sprite = new Sprite(); 74 rect.graphics.beginFill(color,0.9); 75 rect.graphics.drawRect(-width*0.5,-height*0.5,width,height); 76 rect.graphics.endFill(); 77 return rect; 78 } 79 } 80 }