package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Vector3D;
import flash.text.TextField;
import flash.text.TextFormat;
/**
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @create 2015-9-2 下午3:17:09
*
*/
[SWF(width="800",height="600")]
public class DrawCube extends Sprite
{
private var _faceArr:Array = [];
private var _sideLen:Number = 100;//正方形边长
private var _container:Sprite;
private var _text:String = "AS世界你好";
private var _tf:TextFormat = new TextFormat("微软雅黑",60,0xff0000);
public function DrawCube()
{
initViews();
initEventListeners();
}
private function initViews():void
{
_container = new Sprite();
_container.x = 300;
_container.y = 300;
this.addChild(_container);
creatCube();
}
private function initEventListeners():void
{
this.addEventListener(Event.ENTER_FRAME,onMove);
}
private function onMove(e:Event):void
{
for(var i:int = 0;i<6;i++){
_faceArr[i].transform.matrix3D.appendRotation((mouseX-_container.x)/50,Vector3D.Y_AXIS);
_faceArr[i].transform.matrix3D.appendRotation((_container.y-mouseY)/50,Vector3D.X_AXIS);
}
_faceArr.sortOn("z",18);
for(i=0;i<_faceArr.length;i++){
_container.setChildIndex(_faceArr[i],i);
}
}
private function creatCube():void
{
for(var i:int=0;i<6;i++){
var rect:Sprite = drawRect(_sideLen,_sideLen,Math.random()*0xffffff);
var txt:TextField = new TextField();
_container.addChild(rect);
rect.addChild(txt);
txt.autoSize = "center";
txt.selectable = false;
txt.defaultTextFormat = _tf;
txt.text = _text.substr(i,1);
txt.x = -txt.width*0.5;
txt.y = -txt.height*0.5;
rect.z = -_sideLen/2;
_faceArr[i] = rect;
i<4?rect.transform.matrix3D.appendRotation(90*i,Vector3D.Y_AXIS):0;
}
_faceArr[4].transform.matrix3D.appendRotation(-90,Vector3D.X_AXIS);
_faceArr[5].transform.matrix3D.appendRotation(90,Vector3D.X_AXIS);
}
private function drawRect(width:Number,height:Number,color:uint):Sprite
{
var rect:Sprite = new Sprite();
rect.graphics.beginFill(color,0.9);
rect.graphics.drawRect(-width*0.5,-height*0.5,width,height);
rect.graphics.endFill();
return rect;
}
}
}