topical media & game development

talk show tell print

#script-sandy-steve-Basics.ax

#script-sandy-steve-Basics.ax [swf] flex


  // ActionScript file
  package
  {
          //import flash.display.Sprite; 
          import flash.display.*;
          import flash.events.*;
          import flash.ui.*;
          import flash.net.URLRequest;
          import flash.text.*;        
          
          import sandy.core.data.*;
          import sandy.core.Scene3D;
          import sandy.core.scenegraph.*;
          import sandy.materials.*;
          import sandy.materials.attributes.*;
          import sandy.primitive.*;
          import sandy.util.*;
          import sandy.events.*;
          import flash.media.Video;
          import flash.net.NetConnection;
          import flash.net.NetStream;
          
          public class @ax-script-sandy-steve-Basics extends Sprite {
                  private var scene:Scene3D;
                  private var camera:Camera3D;
                  
                  private var tg:TransformGroup; // Create a transformGroup
                  private var tg2:TransformGroup;
                  private var tg3:TransformGroup;
                  private var plane:Plane3D;
                  private var box:Box;
                  private var logo:Max;
                  
                  private var text:TextField = new TextField();
                  private var movie:MovieClip = new MovieClip();
  
                  private var active:Boolean = true;
                  private var start:Boolean = true;
                  private var destroy:Boolean = false;
  
                  public function @ax-script-sandy-steve-Basics() { 
                          // We create the camera
                          camera = new Camera3D( 400, 400 );
                          camera.x = 100;
                          camera.y = 50;
                          camera.z = -600;
                          camera.lookAt(0,0,0);
                          
                          text.width = 200;
                          text.x = 20;
                          text.y = 20;
                   
                          // 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 );
                          importTextures();
                          
                          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();
                          
                          tg = new TransformGroup('plane');
                          tg2 = new TransformGroup('box');
                   
                          // Create a cube so we have something to show
                          plane        = new Plane3D("plane", 200, 150, 1, 1);
                          box         = new Box ("box", 120, 100, 120, PrimitiveMode.QUAD, 1);
                          
                          box.enableBackFaceCulling = false;
                          box.enableNearClipping = true;
                          
                   // we define a new material
                          var mat:MaterialAttributes = new MaterialAttributes (new LightAttributes (false, 0.3));
  
                          logo = new Max ("object");
                          logo.appearance = new Appearance (new ColorMaterial (0x3344CC, 1, mat));
                          logo.appearance.frontMaterial.lightingEnable = true;
                  
                          // Object placement 
                          plane.y         = 135;
                          plane.rotateZ = 90;
                          box.x                = -80;
                          box.y                = -95;
                          logo.x                = 150;
                          logo.y                = -30;
                                                  
                          plane.container.buttonMode = true;
                          box.container.buttonMode = true;
                          
                          // We need to add the cube to the group
                          tg.addChild (plane);
                          tg2.addChild (box);
                                                  
                          g.addChild (logo);
                          g.addChild (tg);
                          g.addChild (tg2);
                    
                          return g;
                  }
  
                  // The Event.ENTER_FRAME event handler tells the world to render
                  private function loop3D (event:Event) : void {
                          plane.container.addEventListener(MouseEvent.CLICK, clickHandlerPlane);
                          plane.container.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
                          box.container.addEventListener(MouseEvent.CLICK, clickHandlerBox);
                          box.container.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
                          logo.container.addEventListener(MouseEvent.CLICK, clickHandlerLogo);
                          logo.container.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
                          stage.addEventListener(KeyboardEvent.KEY_DOWN, activate);
                          stage.addEventListener(Event.ENTER_FRAME, mouseOver);
                          scene.render();
                  }
                  
                  public function importTextures() : void {
                          var importer:Loader = new Loader();
                          importer.contentLoaderInfo.addEventListener(Event.COMPLETE, importCompletedMovie);
                          importer.load(new URLRequest("../assets/student/steve/logo.swf"));
                          
                          var importer2:Loader = new Loader();
                          importer2.contentLoaderInfo.addEventListener(Event.COMPLETE, importCompleted);
                          importer2.load(new URLRequest("../assets/student/steve/vu.png"));
                          
                          var importer3:Loader = new Loader();
                          importer3.contentLoaderInfo.addEventListener(Event.COMPLETE, importCompleted);
                          importer3.load(new URLRequest("../assets/student/steve/realisticCar.png"));
                  }
                  
                  public function importCompleted (event:Event) : void {
                          var target = event.target;
                          var name:String = target.url;
                          name = name.substring(name.lastIndexOf("/")+1).split(".")[0];
                          
                          var texture = target.loader.content.bitmapData;
                          var app:Appearance = new Appearance(new BitmapMaterial (texture));
                          
                          if (name == "realisticCar") {
                                  for (var i:Number = 2; i <  8; i++) {
                                          box.aPolygons[i].appearance = app;
                                  }
                          }
                          else {
                                  box.aPolygons[0].appearance = app;
                                  box.aPolygons[1].appearance = app;
                          }
                          
                  }
                  
                  public function importCompletedMovie (event:Event) : void {
                          var target = event.target;
                          var motor = target.loader.content;
                          movie = new MovieClip();
                          movie.addChild(target.loader.content);
                          var material:MovieMaterial = new MovieMaterial (movie);
                          plane.appearance = new Appearance (material);
                  }
                  
                  private function clickHandlerPlane (event:MouseEvent) : void {
                          text.text = "You have hit the plane"
                          this.addChild(text);
                          destroy = true;
                  }
                  
                  private function clickHandlerBox (event:MouseEvent) : void {
                          text.text = "You have hit the box"
                          this.addChild(text);
                          destroy = true;
                  }
                  
                  private function clickHandlerLogo (event:MouseEvent) : void {
                          text.text = "You have hit the logo."
                          this.addChild(text);
                          destroy = true;
                          active = !active;
                  }
                  
                  private function outHandler (event:MouseEvent) : void {
                          if (destroy) {
                                  this.removeChild(text);
                                  destroy = false;  
                          }
                  }
                  
                  private function activate (event:KeyboardEvent) : void {
                          if(Key.isDown(Keyboard.SPACE)) {
                                  active = !active;
                          } 
                          else if(Key.isDown(Keyboard.NUMPAD_4))                         
                          {
                                  box.rotateY--;
                          }
                          else if(Key.isDown(Keyboard.NUMPAD_6))
                          {
                                  box.rotateY++;
                          }
                          else if(Key.isDown(Keyboard.NUMPAD_2))
                          {
                                  box.rotateX++;
                          }
                          else if(Key.isDown(Keyboard.NUMPAD_8))
                          {
                                  box.rotateX--;
                          }
                  }
                  
                  private function mouseOver (event:Event) : void {
                          if (active) {
                                  logo.rotateX += 2;
                                  logo.rotateY += 2;
                          }
                  }
          }
  }
  
  


(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.