FlashPlayer10の3DでAmazon風に矩形回転

z座標でaddChildを入れ替えることで、
回り込みもうまくできたので、
Amazon風の3Dの矩形回転を作成しました。


しかし、コンテナに乗せて、
コンテナのrotationYを回転させることで、
コンテナにaddChildされているオブジェクトを回転させたため、
ただ単にz座標でソートすることができなくなりました…。


そのため、z座標を求めるという処理を書くことになりました。
なんとなく書いて、動いたため、間違っているかも知れません。
以下がそのプログラムです。


リスト:Main03.as

package {
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	[SWF(width=800, height=600, backgroundColor=0)]

	public class Main03 extends Sprite {
		
		private var conteiner:Sprite = new Sprite();
		private var sh1:Shape = new Shape();
		private var sh2:Shape = new Shape();
		
		private var objs:Array = [sh1, sh2];
		
		public function Main03() {
			conteiner.x = stage.stageWidth/2;
			conteiner.y = stage.stageHeight/2;
			addChild(conteiner);
			
			
			initDesign(sh1, 0xFF0000);
			initDesign(sh2, 0x0000FF);
			
			sh1.x = -300
			sh2.x = 300;
			conteiner.addChild(sh1);
			conteiner.addChild(sh2);
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function initDesign(sh:Shape, color:uint):void {
			sh.graphics.beginFill(color);
			sh.graphics.drawRect(-100, -100, 100, 100);
			sh.graphics.endFill();
		}
		
		private var sort:Array = new Array();
		private function sortZ():void {
			for(var i:int=0; i<objs.length; i++) {
				sort[i] = {"z":Math.sin((conteiner.rotationY+180)/180*Math.PI)*objs[i].x, "im":objs[i]};		
			}
			sort.sortOn("z", Array.DESCENDING|Array.NUMERIC);
			for(var j:int=0; j<objs.length; j++) {
				conteiner.setChildIndex(sort[j].im, j);
			}
		}
		
		private function onEnterFrame(event:Event):void {
			if (mouseX < conteiner.x) {
				conteiner.rotationY -= 1;
			}
			else if (mouseX > conteiner.x) {
				conteiner.rotationY += 1;
			}
			
			for each (var sh:Shape in objs) {
				sh.rotationY = -conteiner.rotationY;
			}
			
			sortZ();
		}
		
	}
}