topical media & game development

talk show tell print

#graphic-flex-image-effects-06-Flex-AnimatedPointAtTest.ax

#graphic-flex-image-effects-06-Flex-AnimatedPointAtTest.ax [swf] [flash] flex


  package {
  
          import flash.events.Event;
          import flash.geom.Matrix3D;
          import flash.geom.Vector3D;
  
          [SWF(width=800, height=600, backgroundColor=0x000000)]
  
          
Builds on PointAtTest by animating the pointer into orientation as opposed to immediately update to point directly at the user-controlled ball.

  
          public class @ax-graphic-flex-image-effects-06-Flex-AnimatedPointAtTest extends graphic_flex_image_effects_06_Flex_PointAtTest {
  
                  private static const INTERPOLATION_RATE:Number = 0.05;
  
                  private var _matrix:Matrix3D;
  
                  
Constructor. Sets up listener for ENTER_FRAME event as well as calls super's constructor.

  
                  public function @ax-graphic-flex-image-effects-06-Flex-AnimatedPointAtTest() {
                          super();
                          addEventListener(Event.ENTER_FRAME, onSpriteEnterFrame);
                  }
  
                  
Updates the pointer, rotating it to point at the current ball position. Instead of super class's implementation, this method uses interpolateTo() to move pointer toward ball, saving end matrix orientation so that orientation can be updated over time in an animation.

  
                  override protected function updatePointer():void {
                          var ballPosition:Vector3D = new Vector3D(_ball.x, _ball.y, _ball.z);
                          // get a clone of the current pointer orientation
                          var pointerTransform:Matrix3D = _pointer.transform.matrix3D.clone();
                          // save the matrix (which will be transformed next) to be used in the animation
                          _matrix = pointerTransform.clone();
                          // the desired end orientation of the pointer
                          pointerTransform.pointAt(
                                  ballPosition,
                                  Vector3D.X_AXIS,
                                  new Vector3D(0, -1, 0)
                          );
                          // transform the matrix so that it is slightly oriented towards end matrix
                          _matrix.interpolateTo(pointerTransform, INTERPOLATION_RATE);
                  }
  
                  
Handler for ENTER_FRAME. Performs the orientation animation.
parameter: event Event dispatched by this sprite.

  
                  private function onSpriteEnterFrame(event:Event):void {
                          // if the matrix has been created, which means pointer needs to be oriented
                          // towards the ball, assign it to the pointer, then call updatePointer() again
                          // to interpolate the matrix more towards the ball
                          if (_matrix) {
                                  _pointer.transform.matrix3D = _matrix;
                                  updatePointer();
                          }
                  }
  
          }
  
  }
  


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