topical media & game development

talk show tell print

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

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


  package {
  
          import flash.display.Sprite;
          import flash.events.KeyboardEvent;
          import flash.events.MouseEvent;
          import flash.filters.BlurFilter;
          import flash.filters.GlowFilter;
          import flash.geom.PerspectiveProjection;
          import flash.geom.Point;
          import flash.geom.ColorTransform;
          import flash.ui.Keyboard;
          import flash.text.TextField;
          import flash.text.TextFieldAutoSize;
          import flash.text.TextFormat;
  
          [SWF(width=800, height=600, backgroundColor=0x000000)]
  
          
Gives appearance of extruded text by duplicating textfield and placing copies at different, but close, z depths. Shows how angle and depth of extrusion can be manipulated through user interaction.

  
          public class @ax-graphic-flex-image-effects-06-Flex-ExtrudeText extends Sprite {
  
                  private static const EXTRUDE_DEPTH:uint = 30;
                  private static const Z_DIST_BETWEEN_ITEMS:uint = 5;
  
                  
Constructor. Creates and places multiple copies of textfield and sets up listeners for mouse and key events.

  
                  public function @ax-graphic-flex-image-effects-06-Flex-ExtrudeText() {
                          // creates fresh projection so that it can be accessed later
                          transform.perspectiveProjection = new PerspectiveProjection();
                          for (var i:uint = 0; i < EXTRUDE_DEPTH; i++) {
                                  createField(i);
                          }
                          stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
                          stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
                  }
  
                  
Creates a textfield at the specified index position, which determines its depth.
parameter: index The index position of the field.

  
                  private function createField(index:uint):void {
                          var field:TextField = new TextField();
                          field.selectable = false;
                          field.autoSize = TextFieldAutoSize.LEFT;
                          // be sure to use different font if Impact is not installed
                          field.defaultTextFormat = new TextFormat("Impact", 80, 0x66AAFF);
                          field.text = "EXTRUDE TEST";
                          // center field
                          field.x = stage.stageWidth/2 - field.width/2;
                          field.y = stage.stageHeight/2 - field.height/2;
                          // depth is based on index position
                          field.z = index*Z_DIST_BETWEEN_ITEMS;
                          // for top field, add slight glow
                          if (index == 0) {
                                  field.filters = [new GlowFilter(0xFFFFFF, .5, 6, 6, 2, 1, true)];
                          // for all others, make a non-blur, which caches field into bitmap;
                          // also, darken the field the further back it is, creating nice gradient extrusion
                          } else {
                                  field.filters = [new BlurFilter(0, 0)];
                                  var darken:Number = .1 + (EXTRUDE_DEPTH-index)/EXTRUDE_DEPTH*.8;
                                  field.transform.colorTransform = new ColorTransform(darken, darken, darken);
                          }
                          // add bottom of display list, below previous fields
                          addChildAt(field, 0);
                  }
  
                  
Handler for when mouse moves around stage. This updates the center of the projection, which affects the extrusion angle.
parameter: event Event dispatched by stage.

  
                  private function onStageMouseMove(event:MouseEvent):void {
                          var x:Number = stage.mouseX;
                          var y:Number = stage.mouseY;
                          transform.perspectiveProjection.projectionCenter = new Point(x, y);
                          event.updateAfterEvent();
                  }
  
                  
Handler for when a key is pressed. If the key is the up or down arrow, The field of view is updated in the projection, which affects the extrusion depth.
parameter: event Event dispatched by stage.

  
                  private function onStageKeyDown(event:KeyboardEvent):void {
                          var projection:PerspectiveProjection = transform.perspectiveProjection;
                          var fieldOfView:Number = projection.fieldOfView;
                          switch (event.keyCode) {
                                  case Keyboard.UP:
                                          fieldOfView += 2;
                                          break;
                                  case Keyboard.DOWN:
                                          fieldOfView -= 2;
                                          break;
                          }
                          // keep field of view between .1 and 179.9 to prevent exceptions
                          projection.fieldOfView = Math.max(.1, Math.min(fieldOfView, 179.9));
                          event.updateAfterEvent();
                  }
  
          }
  
  }


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