topical media & game development

talk show tell print

script-sandy-steve-IntroSandy.ax

script-sandy-steve-IntroSandy.ax (swf ) [ flash ]


  package
  {
          import flash.display.Sprite; 
          import flash.events.*;
          import flash.ui.*;
          import flash.ui.Keyboard;
          import sandy.core.data.*;
          import sandy.core.Scene3D;
          import sandy.core.scenegraph.*;
          import sandy.materials.*;
          import sandy.materials.attributes.*;
          import sandy.primitive.*;
       
          public class script-sandy-steve-IntroSandy extends Sprite 
          {
                  private var scene:Scene3D;
                  private var camera:Camera3D;
                  private var tg:TransformGroup; // Create a transformGroup
                  private var box:Box;
                  private var box2:Box;
                  private var active:Boolean = false;
                  private var start:Boolean = false;
           
                  public function script-sandy-steve-IntroSandy()
                  { 
                          // We create the camera
                          camera = new Camera3D( 500, 500 );
                          camera.x = 100;
                          camera.y = 100;
                          camera.z = -400;
                          camera.lookAt(0,0,0);
                   
                          // We create the "group" that is the tree of all the visible objects
                          var root:Group = createScene();
                   
                          // We create a Scene and we add the camera and the objects tree 
                          scene = new Scene3D( "scene", this, camera, root );
                          
                          Key.initialize(stage);
                   
                          // Listen to the heart beat and render the scene
                          addEventListener( Event.ENTER_FRAME, loop3D );    
                  }
  
                  // Create the scene graph based on the root Group of the scene
                  private function createScene():Group
                  {
                          // Create the root Group
                          var g:Group = new Group();
                          
                          // create axis
                          var xLine:Line3D = new Line3D("xAxis", new Vector(-70, 0, 0), new Vector (70, 0, 0));
                          var yLine:Line3D = new Line3D("yAxis", new Vector(0, -70, 0), new Vector (0, 70, 0));                        
                          var zLine:Line3D = new Line3D("zAxis", new Vector(0, 0, -110), new Vector (0, 0, 110));
                          
                          tg = new TransformGroup('testGroup');
                   
                          // Create a cube so we have something to show
                          box = new Box( "box",50,50,50,"quad");
                          box2 = new Box("box2", 25, 25, 25, "quad");
           
                   // we define a new material
                          var materialAttr:MaterialAttributes = new MaterialAttributes( 
                                  new LineAttributes( 1, 0x000000, 0.5 ),
                                  new LightAttributes( true, 0.4));
  
                          var material:Material = new ColorMaterial( 0x006633, .8, materialAttr );
                          var material2:Material = new ColorMaterial( 0xFF3399, .8, materialAttr );
                          material.lightingEnable = false;
                          
                          var matAttrYLine:MaterialAttributes = new MaterialAttributes( 
                                  new LineAttributes( 1, 0xFFFF33, 1 ));
                                  
                          var matAttrXLine:MaterialAttributes = new MaterialAttributes( 
                                  new LineAttributes( 1, 0xCC0000, 1 ));
                                  
                          var matAttrZLine:MaterialAttributes = new MaterialAttributes( 
                                  new LineAttributes( 1, 0x0000CC, 1 ));
                          
                          box.appearance = new Appearance (material);
                          box2.appearance = new Appearance (material2);
                          xLine.appearance = new Appearance (new ColorMaterial ( 0, 1 , matAttrXLine));
                          yLine.appearance = new Appearance (new ColorMaterial ( 0, 1 , matAttrYLine));
                          zLine.appearance = new Appearance (new ColorMaterial ( 0, 1 , matAttrZLine));
                  
                          // Object placement 
                          box.rotateX = 35;
                          box.rotateY = 35;
                          box2.y = 75;
                          //box2.moveUpwards(80);
                                    
                          // We need to add the cube to the group
                          g.addChild( box );
                          tg.addChild (box2);
                          g.addChild(xLine);
                          g.addChild(yLine);
                          g.addChild(zLine);
                          
                          g.addChild(tg);
                    
                          return g;
                  }
  
                  // The Event.ENTER_FRAME event handler tells the world to render
                  private function loop3D (event:Event) : void
                  {
                          stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
                          stage.addEventListener(MouseEvent.CLICK, startCamera);
                          stage.addEventListener(MouseEvent.MOUSE_OVER, activate);
                          stage.addEventListener(Event.ENTER_FRAME, mouseOver);
                          stage.addEventListener(Event.ENTER_FRAME, moveCamera);
                          scene.render();
                  }
                  
                  private function keyPressed (event:KeyboardEvent) : void 
                  {
                          if (Key.isDown(Keyboard.NUMPAD_8)) 
                                  box.rotateX -= 6;
                          if (Key.isDown(Keyboard.NUMPAD_2)) 
                                  box.rotateX += 6;
                          if (Key.isDown(Keyboard.NUMPAD_6)) 
                                  box.rotateY += 6;
                          if (Key.isDown(Keyboard.NUMPAD_4)) 
                                  box.rotateY -= 6;
                          if (Key.isDown(Keyboard.UP))
                                  box.z += 6;
                          if (Key.isDown(Keyboard.DOWN))
                                  box.z -= 6;
                          if (Key.isDown(Keyboard.RIGHT))
                                  box.x += 6;
                          if (Key.isDown(Keyboard.LEFT))
                                  box.x -= 6;
                          if (Key.isDown(Keyboard.NUMPAD_ADD))
                                  box.y += 6;
                          if (Key.isDown(Keyboard.NUMPAD_SUBTRACT))
                                  box.y -= 6;
                          
                  /*	if (event.keyCode == Keyboard.NUMPAD_8) 
                                  box.rotateX -= 6;
                          if (event.keyCode == Keyboard.NUMPAD_2) 
                                  box.rotateX += 6;
                          if (event.keyCode == Keyboard.NUMPAD_6) 
                                  box.rotateY += 6;
                          if (event.keyCode == Keyboard.NUMPAD_4) 
                                  box.rotateY -= 6;
                          if (event.keyCode == Keyboard.UP)
                                  box.z += 6;
                          if (event.keyCode == Keyboard.DOWN)
                                  box.z -= 6;
                          if (event.keyCode == Keyboard.RIGHT)
                                  box.x += 6;
                          if (event.keyCode == Keyboard.LEFT)
                                  box.x -= 6;
                          if (event.keyCode == Keyboard.NUMPAD_ADD)
                                  box.y += 6;
                          if (event.keyCode == Keyboard.NUMPAD_SUBTRACT)
                                  box.y -= 6;*/
                  }
                  
                  private function activate (event:MouseEvent) : void
                  {
                          active = !active;
                  }
                  private function mouseOver (event:Event) : void 
                  {
                          if (active)
                          {
                                  box.rotateX++;
                                  tg.rotateZ++;
                                  tg.rotateX++;
                                  tg.pan += 3;        
                          }
                  }
                  
                  private function startCamera (event:MouseEvent) : void
                  {
                          start = !start;
                  }
                  
                  private function moveCamera (event:Event) : void
                  {
                          if (start)
                          {
                                  camera.roll += 1;
                          }
                  }
                  
          }
  }
  
  


(C) Æliens 20/2/2008

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.