Tuesday, December 7, 2010

Starting Over

Hello hello again dear readers!  I do owe you quite an update, but unfortunately I haven't had to time or energy to do so adequately.  Suffice to say, I will sometime near the end of this week, as promotion for my Android app is beginning sometime between tomorrow and Wednesday.  That will feature all of our new functionality in video format, so you'll get another nice run-through of what's been going on since my last in-depth feature summary.

To tide you over until then, I'm going to drop off a base Away3D file that I recently wrote for my internship to help one of their Flash designers get into the swing of things.  It's a basic start-up file that handles some input and is ready to have objects added to it for display.  It's heavily commented and (hopefully!) logically arranged, so it's good for any beginner that wants to wade through it.  Enjoy!



// A3DCrane.as
// Author: Jay Domagala
// Last Modified: 12/6/2010

package
{
 // Away3D imports
 import away3d.cameras.HoverCamera3D;
 import away3d.cameras.lenses.PerspectiveLens;
 import away3d.containers.Scene3D;
 import away3d.containers.View3D;
 import away3d.core.clip.Clipping;
 import away3d.core.clip.FrustumClipping;
 import away3d.core.render.Renderer;
 import away3d.debug.AwayStats;
 
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.events.KeyboardEvent;
 import flash.events.MouseEvent;
 import flash.geom.Point;
 
 // This is how you set the SWF properties in code
 // I've provided the four most common/important properties
 [SWF(frameRate="30", backgroundColor="#000000", width="800", height="600")]
 
 // All of your base classes for display in an Away3D project extend Sprite
 // This is so they can write all the artificial 3D information into the stage's display list
 // You don't have to worry too much about it
 public class A3DCrane extends Sprite
 {
  // As usual, any and all member variables go here, before the constructor
  // Technically, you can initialize them anywhere within the class declaration
  // I find that to be highly obnoxious and messy
  // Then again, I'm a little OCD
  
  // Away3D requirements
  private var scene:Scene3D;
  private var camera:HoverCamera3D;
  private var clipping:Clipping;
  private var view:View3D;
  
  // 3D Variables
  
  // 2D Variables
  
  // Input Variables
  private var keyDown:Array;
  private var lastMousePoint:Point;
  
  // Camera Variables
  private var move:Boolean = false;
  private var lastPanAngle:Number = 0;
  private var lastTiltAngle:Number = 0;
  
  // A3DCrane
  // Parameters: None
  // Initializes the class on start-up
  public function A3DCrane()
  {
   // For readability's sake, I pull out a lot of functionality into separate functions
   // Just make sure they're named well and you have nothing to worry about
   initAway3D();
   initStats();
   
   // To make everything render, we need to add a view.render() call every frame
   // So, make sure to add an EventListener for it
   addEventListener(Event.ENTER_FRAME, updateLoop);
   
   // If we want input, call the initInput function
   initInput();
   
  }// A3DCrane constructor
  
  // updateLoop
  // Parameters: Event - ENTER_FRAME
  // Handles the frame by frame logic and rendering
  private function updateLoop(e:Event):void
  {
   // Call updateCamera every frame to make sure it's updating when necessary
   updateCamera();
   
   // Show us what we've got!
   view.render();
  }// updateLoop
  
  // updateCamera
  // Parameters: None
  // Used for camera updates regarding smoothing without using Tweens
  private function updateCamera() : void
  {
   // For reference:
   // Pan is rotation along the Y-axis
   // Tilt is rotation along the Z-axis
   
   // This variable can be altered to adjust the mouse-to-camera sensitivity
   var speed:Number = 0.3;
   
   // If we're dragging the camera around
   if (move) 
   {
    // Our new angle is the distance between our current mouse position and the previous
    // Scaled by our speed variable
    // Plus our previous angle
    camera.panAngle = speed * (stage.mouseX - lastMousePoint.x) + lastPanAngle;
    camera.tiltAngle = speed * (stage.mouseY - lastMousePoint.y) + lastTiltAngle;
   }// if move
   
   // And then we have to make sure to tell it to hover before leaving
   camera.hover();
  }// updateCamera
  
  // initAway3D
  // Parameters: None
  // Initializes the Scene, View, Camera and Clipping
  private function initAway3D() : void
  {
   // Almost, if not all, Away3D objects take in an optional parameter list
   // Sometimes it's nice to use it, other times you can't
   
   // For this scene, we won't be using the parameter list due to uninitialized variables needed
   scene = new Scene3D();
   
   //
   camera = new HoverCamera3D({zoom:4, focus:100, panAngle: -180, tiltAngle: 15, x:0, y:35, z:-75});
   camera.lens = new PerspectiveLens();
   
   // This clipping doesn't need paramters as it's a specific type of clipping
   // It already knows a lot of what we want to do
   clipping = new FrustumClipping();
   
   // Almost any variables and properties can be set in the parameter list
   // The view will take in all of our initialized set-up variables
   // It will also take in values for the x and y
   // Since this is the viewport, and is essentially a Sprite, there is no z value
   // NOTE: Renderer.CORRECT_Z_ORDER will do a proper Z-sort on all objects but makes it run slower
   // Use it intelligently
   view = new View3D({scene:scene, camera:camera, clipping:clipping, renderer:Renderer.CORRECT_Z_ORDER, x:250, y:200});
   
   // Make sure to add the view to the stage or you won't see anything
   addChild(view);
  }// initAway3D
  
  // initStats
  // Parameters: None
  // Initializes the stats window and adds it to the stage
  private function initStats() : void
  {
   // Unlike most Away3D objects, AwayStats does not take in a parameter list
   // The only one that you need for our purposes is the view
   var awaystats:AwayStats = new AwayStats(view);
   awaystats.x = 10;
   awaystats.y = 10;
   
   // We add this to the stage and not the view
   // All 2D objects should be added this way
   addChild(awaystats);
  }// initStats
  
  // initKeyboardArray
  // Parameters: None
  // Sets up the array that will be used to check keyboard input
  private function initInput() : void
  {
   // Naturally we have to create the Array for our keys
   keyDown = new Array();
   
   // Then we add all the keys we care about
   // We're doing this with programming 'keys'
   // That is, indexes in our Array that we can more easily read and look up
   keyDown["w"] = false;
   keyDown["a"] = false;
   keyDown["s"] = false;
   keyDown["d"] = false;
   
   // And for good measure, let's set our listeners
   // As usual, the listeners are just added to the stage
   addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler);
   addEventListener(KeyboardEvent.KEY_UP, keyboardHandler);
   
   // The mouse stuff is a lot easier
   lastMousePoint = new Point();
   
   addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
   addEventListener(MouseEvent.MOUSE_UP, mouseHandler);
  }// initKeyboardArray
  
  // keyboardHandler
  // Parameters: KeyboardEvent - KEY_UP, KEY_DOWN
  // Catches all the KEY_UP and KEY_DOWN events
  // Flips the Booleans in the key Array as necessary
  private function keyboardHandler(e:KeyboardEvent):void
  {
   // If the event was a KEY_DOWN
   if (e.type == KeyboardEvent.KEY_DOWN)
   {
    // If we initialized that key, take note of the press
    if (keyDown[String.fromCharCode(e.charCode)] != null)
     keyDown[String.fromCharCode(e.charCode)] = true;
   }// if e.type == KEY_DOWN
   
   // If the event was a KEY_UP
   if (e.type == KeyboardEvent.KEY_UP)
   {
    // If we initialized that key, take note of the release
    if (keyDown[String.fromCharCode(e.charCode)]!= null)
     keyDown[String.fromCharCode(e.charCode)] = false;
   }// if e.type == KEY_UP
  }// keyboardHandler
  
  
  // mouseHandler
  // Parameters: MouseEvent - MOUSE_UP, MOUSE_DOWN
  // Handles the mouse input as necessary
  private function mouseHandler(e:MouseEvent) : void
  {
   // If the mouse is down, we start dragging the camera
   if (e.type == MouseEvent.MOUSE_DOWN)
   {
    // We need to update all our variables used for smoothing
    lastPanAngle = camera.panAngle;
    lastTiltAngle = camera.tiltAngle;
    lastMousePoint.x = stage.mouseX;
    lastMousePoint.y = stage.mouseY;
    
    // And let our update function know it needs to adjust the camera
    move = true;
   }// if e == MOUSE_DOWN
   
   // If the mouse is up, we stop affecting the camera
   if (e.type == MouseEvent.MOUSE_UP)
    move = false;
  }// mouseHandler
 }// A3DCrane declaration
}// package

No comments:

Post a Comment