fp10 Api中3D
程序员文章站
2022-03-16 15:10:59
...
package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Vector3D;
[SWF(backgroundColor=0xffffff,width='1024',height='600')]
public class Carousel extends Sprite {
private var _holder:Sprite;
private var _items:Array;
private var _radius:Number = 200;
private var _numItems:int = 30;
public function Carousel() {
//root.transform.perspectiveProjection.focalLength = 0;
_holder = new Sprite();
_holder.x = stage.stageWidth/2;
_holder.y = stage.stageHeight/2;
_holder.z = 0;
this.addChild(_holder);
_items = new Array();
for( var i:int = 0; i<_numItems; i++ ) {
var angle:Number = Math.PI*2/_numItems * i;
var item:Sprite = creatItem();
item.x = 2*Math.cos(angle) * _radius;
item.z = 2*Math.sin(angle) * _radius;
//item.y = 2*Math.sin(angle) * _radius;
item.rotationY = -360/_numItems * i //+ 90;
_items.push(item);
}
sortItems();
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
//深度排序
private function sortItems():void {
_items.sort(depthSort);
for( var i:int = 0; i<_items.length; i++ ) {
_holder.setChildIndex( _items[i] as Sprite, i );//.addChildAt( _items[i] as Sprite, i );
}
}
private function depthSort(objA:DisplayObject, objB:DisplayObject):int {
var posA:Vector3D = objA.transform.matrix3D.position;
posA = _holder.transform.matrix3D.deltaTransformVector(posA);
var posB:Vector3D = objB.transform.matrix3D.position;
posB = _holder.transform.matrix3D.deltaTransformVector(posB);
return posB.z - posA.z;
}
private function creatItem():Sprite {
var item:Sprite = new Sprite();
item.graphics.beginFill(Math.random()*0xffffff);
item.graphics.drawRect(-50,-50,100,100);
_holder.addChild(item);
return item;
}
//item绕Y轴旋转
private function onEnterFrame(e:Event):void {
_holder.rotationY += (stage.stageWidth/2 - this.mouseX)*.01;
_holder.y += (mouseY - _holder.y) * .1;
sortItems();
}
}
}