package be.nascom.flash10_tests.widgets{ import flash.display.Sprite; /** * An extremely simple implementation of a rotating 3d cube menu * uses only rudimentary 3d features (rotateX, rotateY) */ public class graphic_player_10_cube_be_nascom_flash10_tests_widgets_SimpleCube extends Sprite{ //public static const rotation_targets:Array=new Array(); protected static var _rotation_offsets:Array; protected var _sides:Array; protected var _sort_pairs:Array; public function graphic_player_10_cube_be_nascom_flash10_tests_widgets_SimpleCube(materials:Array){ super(); init(materials); } public function validateMaterials(materials:Array):void{ //throw error } protected function getRotationOffsets(size:uint):Array{ var h:int=size/2; //ORDER front, back, left, right, top, bottom //values x,y,z,rotationX,rotationY,rotationZ return new Array( new Offset3d( 0, 0, h, 0, 0, 0), new Offset3d( 0, 0, -h, 0, 180, 0), new Offset3d( -h, 0, 0, 0, 90, 0), new Offset3d( h, 0, 0, 0, 270, 0), new Offset3d( 0, -h, 0, 270, 0, 0), new Offset3d( 0, h, 0, 90, 0, 0) ); } public function init(materials:Array):void{ validateMaterials(materials); var size:uint=materials[0].width;//height should be the same _rotation_offsets=getRotationOffsets(size); //trace("size:"+size); _sides=new Array(6); _sort_pairs=new Array(6); var side:graphic_player_10_cube_be_nascom_flash10_tests_widgets_SimpleCubeSide; for(var i:uint=0;i<6;i++){ side=new graphic_player_10_cube_be_nascom_flash10_tests_widgets_SimpleCubeSide(size,materials[i],_rotation_offsets[i]); addChild(side); _sides[i]=side; _sort_pairs[i]=new graphic_player_10_cube_be_nascom_flash10_tests_widgets_SimpleCubeSortPair(side,0); } } public function rotate( x_rotation:Number, y_rotation:Number, z_rotation:Number):void{ this.rotationX+=x_rotation; this.rotationY+=y_rotation; this.rotationZ+=z_rotation; zSort(); } protected function zSort():void{ var pair:graphic_player_10_cube_be_nascom_flash10_tests_widgets_SimpleCubeSortPair; for each(pair in _sort_pairs){ //if(pair.side==null)return; pair.z=pair.side.material.transform.getRelativeMatrix3D(parent).position.z; removeChild(pair.side); } _sort_pairs.sortOn("z", Array.NUMERIC | Array.DESCENDING); var i:uint=0; for(i=0;i<6;i++){ addChild(graphic_player_10_cube_be_nascom_flash10_tests_widgets_SimpleCubeSortPair(_sort_pairs[i]).side); } } public function destroy():void{ //remove all sides for(var i:uint=0;i<6;i++){ removeChild(_sides[i]); _sides[i]=null; } _sort_pairs=null; _sides=null; } } }