从Flash Player 10, AIR 1.5开始,flash中的显示对象就已经支持3d属性了。虽然是使用cpu运算,但是做一些简单而华丽的效果(比如3d照片墙),还是很好用的。
只是用到了显示对象的x、y、z、rotationX、rotationY属性,开发起来很方便。效果也不错。想比于使用GPU的技术,编译速度和初始化速度要快很多。
代码如下:
package { import fl.controls.CheckBox; import fl.controls.NumericStepper; import fl.controls.Slider; import flash.display.MovieClip; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; public class Main extends MovieClip { public var _xNumric:NumericStepper; public var _yNumric:NumericStepper; public var _zNumric:NumericStepper; public var _cardNumric:NumericStepper; public var _aNumric:NumericStepper; public var _bNumric:NumericStepper; public var _rotateCheck:CheckBox; public var _halfCheck:CheckBox; public var _doorCheck:CheckBox; public var _rotateSpeed:NumericStepper; public var _doorOffsetX:NumericStepper; public var _doorOffsetY:NumericStepper; public function Main() { // constructor code stage?initStage(null):addEventListener(Event.ADDED_TO_STAGE, initStage); } private function initStage(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE, initStage); stage.scaleMode = StageScaleMode.NO_SCALE; var con:Sprite = new Sprite(); con.x = 500; addChildAt(con, 0); var doorCon:Sprite = new Sprite(); addChildAt(doorCon, 0); doorCon.x = 500; var doorL:Door = new Door(); doorCon.addChild(doorL); var doorR:Door = new Door(); doorCon.addChild(doorR); doorL.z = doorR.z = 0; var i:int = 0; var cardArr:Array = []; var num:int; var n:Number = 0; function addCard() { con.removeChildren(); cardArr = []; for (i = 0; i < num;i++ ) { var card:Card = new Card(); con.addChild(card); cardArr.push(card); } } addEventListener(Event.ENTER_FRAME, enterFrameHandler); function enterFrameHandler(e:Event):void { if (_rotateCheck.selected) { n+=_rotateSpeed.value; } if (num != _cardNumric.value) { num = _cardNumric.value; addCard(); } var a:Number = _aNumric.value; var b:Number = _bNumric.value; var halfFlag:Boolean = _halfCheck.selected; doorCon.visible = _doorCheck.selected; if (n > 360) n -= 360; for (i = 0; i < num;i++ ) { var card:Card = cardArr[i]; var _n:Number = (n + i * 360 / num); var angle:Number = _n * Math.PI / 180; card.x = a * Math.cos(angle); card.y = 0; card.z = b * Math.sin(angle); if (halfFlag) { card.visible = (card.z<=0); } else { card.visible = true; } //card.rotationY = (180-_n+360)%180-90;//圆 //var k:Number = -b * b * card.x / (a * a * card.y);//斜率 var theta:Number = Math.atan2(-b * b * card.x,a * a * card.z); card.rotationY = -theta * 180 / Math.PI; } //排序,按z值降序排列 cardArr.sortOn("z", Array.NUMERIC|Array.DESCENDING); for (i = 0; i < num;i++ ) { con.addChild(cardArr[i]); } doorCon.y = con.y = _yNumric.value; doorCon.z = con.z = _zNumric.value; doorCon.rotationX = con.rotationX = _xNumric.value; doorR.x = a+_doorOffsetX.value; doorL.x = -(a + _doorOffsetX.value); doorL.y = doorR.y = _doorOffsetY.value; } } } }
效果:
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。