topical media & game development

talk show tell print

#graphic-player-10-cube-be-nascom-flash10-tests-CubeTest.ax

#graphic-player-10-cube-be-nascom-flash10-tests-CubeTest.ax [swf] [flash] flex


  package be.nascom.flash10_tests{
          
          import be.nascom.flash.threeD.MouseRotator;
          import be.nascom.flash10_tests.controls.CubeMaterialToggle;
          import be.nascom.flash10_tests.widgets.CubeSideMaterial;
          import be.nascom.flash10_tests.widgets.MouseRotationTracer;
          import be.nascom.flex.component.FlexSimpleTraceBox;
          
          import flash.display.DisplayObject;
          import flash.display.Sprite;
          import flash.events.Event;
          import flash.events.MouseEvent;
          import flash.geom.Point;
          import flash.text.TextField;
          import flash.text.TextFormat;
  
          public class @ax-graphic-player-10-cube-be-nascom-flash10-tests-CubeTest extends AbstractFP10Test{
                  
                  protected var _cube_holder:Sprite;
                  protected var _mouse_rotator:MouseRotator;
                  
                  public function @ax-graphic-player-10-cube-be-nascom-flash10-tests-CubeTest(){
                          super();
                          FlexSimpleTraceBox.trace("@ax-graphic-player-10-cube-be-nascom-flash10-tests-CubeTest()");
                  }
                  
                  
                  protected var _material_function:Function=createSideWithColorMaterial;
                  protected var _material_selector:CubeMaterialToggle;
                  override protected function createChildren():void{
                          super.createChildren();
                          _material_selector=new CubeMaterialToggle();
                          _material_selector.addEventListener(CubeMaterialToggle.USE_COLOR_MATERIAL,useColorMaterial);
                          _material_selector.addEventListener(CubeMaterialToggle.USE_WIREFRAME_MATERIAL,useWireframeMaterial);
                          _material_selector.x=10;
                          _material_selector.y=80;
                          addChild(_material_selector);
                  }
                  
                  override public function run():void{
                          FlexSimpleTraceBox.trace("@ax-graphic-player-10-cube-be-nascom-flash10-tests-CubeTest.run()");
                          createCube();
                          startRotateCube();
                          root.transform.perspectiveProjection.projectionCenter = test_holder.localToGlobal(new Point(400,400));
                  }
                  
                  protected var sides:Array
                  protected function createCube():void{
                          removeCube();
                          _cube_holder=new Sprite();
                          _cube_holder.x=400;
                          _cube_holder.y=200;
                          _cube_holder.z=100;
                          test_holder.addChild(_cube_holder);
                          
                          var size:uint=200;
                          sides=new Array(6);
                          for(var i:uint=0;i<6;i++){
                                  sides[i]=_material_function(size,i);
                                  _cube_holder.addChild(sides[i]);
                          }
                          //front and back
                          var cube1:DisplayObject=sides[0];
                          var cube2:DisplayObject=sides[1];
                          cube1.z=size/2;
                          cube2.z=-size/2;
                          
                          //top and bottom
                          cube1=sides[2];
                          cube2=sides[3];
                          cube1.rotationX=cube2.rotationX=90;
                          cube1.y=-size/2;
                          cube2.y=size/2;
                          
                          //left and right
                          cube1=sides[4];
                          cube2=sides[5];
                          cube1.rotationY=cube2.rotationY=90;
                          cube1.x=-size/2;
                          cube2.x=size/2;
                  }
                  
                  protected function createSideWithColorMaterial(size:uint,num:uint):CubeSideMaterial{
                          return new CubeSideMaterial(size,size,num);
                  }
                  protected function createSideWithWireFrameMaterial(size:uint,num:uint):Sprite{
                          var half:int=-size/2;
                          var s:Sprite=new Sprite();
                          s.graphics.lineStyle(2,0x000000,1);
                          s.graphics.drawRect(half,half,size,size);
                          s.useHandCursor=true;
                          s.buttonMode=true;
                          var tf:TextField=new TextField();
                          tf.selectable=false;
                          var tform:TextFormat=new TextFormat();
                          tform.size=42;
                          tform.font="Verdana";
                          tform.bold=true;
                          tf.text=String(num);
                          tf.setTextFormat(tform);
                          s.addChild(tf);
                          return s;
                  }                
  
                  protected var _rotation_tracker:MouseRotationTracer;
                  
                  protected function rotateCube(e:Event):void{
                          //trace("@ax-graphic-player-10-cube-be-nascom-flash10-tests-CubeTest.rotateCube()");
                          //_rotation_tracker.update(_mouse_rotator.x_rotation_speed,_mouse_rotator.y_rotation_speed,_mouse_rotator.z_rotation_speed);
                          _cube_holder.rotationX+=_mouse_rotator.x_rotation_speed;
                          _cube_holder.rotationY+=_mouse_rotator.y_rotation_speed;
                          _cube_holder.rotationZ+=_mouse_rotator.z_rotation_speed;
                  }
                  
  
                  
                  protected function startRotateCube():void{
                          //addEventListener(Event.ENTER_FRAME,rotateCube);
                          trace("@ax-graphic-player-10-cube-be-nascom-flash10-tests-CubeTest.startRotateCube()");
                          _mouse_rotator=new MouseRotator(0,0,800,800,5);
                          //_mouse_rotator.addEventListener(Rotation3dEvent.ROTATION_COMMAND,rotateCube);
                          stage.addEventListener(MouseEvent.MOUSE_MOVE,_mouse_rotator.update);
                          addEventListener(Event.ENTER_FRAME,rotateCube);
                          /*
                          _rotation_tracker=new MouseRotationTracer();
                          addChild(_rotation_tracker);
  			*/        
                  }
                  
                  protected function stopRotateCube():void{
                          //_cube_holder.removeEventListener(Rotation3dEvent.ROTATION_COMMAND,rotateCube);
                          stage.removeEventListener(Event.ENTER_FRAME,_mouse_rotator.update);
                  }
                  
                  protected function removeCube():void{
                          if(_cube_holder!=null){
                                  if(test_holder.contains(_cube_holder))test_holder.removeChild(this._cube_holder);                
                          }
                  }
                  
                  protected function useColorMaterial(e:Event=null):void{
                          _material_function=createSideWithColorMaterial;
                          createCube();
                  }
                  protected function useWireframeMaterial(e:Event=null):void{
                          _material_function=createSideWithWireFrameMaterial;
                          createCube();
                  }
                  
                  override public function destroy():void{
                          _material_selector.removeEventListener(CubeMaterialToggle.USE_COLOR_MATERIAL,useColorMaterial);
                          _material_selector.removeEventListener(CubeMaterialToggle.USE_WIREFRAME_MATERIAL,useWireframeMaterial);
                          removeChild(_material_selector);                        
                          removeCube();
                          stopRotateCube();
                          FlexSimpleTraceBox.trace("@ax-graphic-player-10-cube-be-nascom-flash10-tests-CubeTest.destroy()");
                  }
                  
                  //possibility for developer to document findings, important items to note etc.  Use HTML.
                  override public function getNotes():String{
                          var note:String="About the 3d stacking...";
                          note+="<ul>";
                          note+="<li>This is set up using x, y, z, and rotation properties.</li>";
                          note+="<li>The sides are nested 'SimpleButtons' in a Sprite which is being rotated based on mouse coords.</li>";
                          note+="<li>There is no z sorting, so this requires looking at the optical illusion 'the right way'</li>";
                          note+="<li>The wireframe version is more convincing ;)</li>";
                          note+="<li>Notice the mouse overs have some funny behaviour, but I suspect this is due to the lack of sorting</li>";
                          note+="<li>I'm sure there's a 'flashy' shortcut for the z sorting...</li>";
                          note+="</ul>";
                          return note;
                  }
                  
          }
  }


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.