topical media & game development

talk show tell print

graphic-flex-draw-classes-DrawingGradientFills.ax

graphic-flex-draw-classes-DrawingGradientFills.ax [swf] flex


  package {
  
          import flash.display.GradientType;
          import flash.display.Sprite;
          import flash.events.MouseEvent;
          import flash.events.*;
          import flash.geom.Matrix;
          import flash.display.Stage;
  
          [SWF(width=550, height=400, backgroundColor=0xFFFFFF)]
  
          
Demonstrates the drawing of a radial gradient fill that updates as the mouse moves.

  
          public class @ax-graphic-flex-draw-classes-DrawingGradientFills extends Sprite {
  
                  
Constructor. This establishes the MOUSE_MOVE listener and draws the background initially.

  
                  public function @ax-graphic-flex-draw-classes-DrawingGradientFills() {
                          super();
                          this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
                  }
                  
                  private function onAddedToStage(e:Event):void
                  {
                          this.init();
                  }
                  
                  private function init():void
                  {
                          stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
                          drawBackground();
                  }
  
                  
Draws the background with a radial gradient.

  
                  private function drawBackground():void {
                          var colors:Array = [0xFFFF00, 0xFF0000, 0x000000];
                          var alphas:Array = [1, 1, 1];
                          var ratios:Array = [50, 100, 255];
                          var matrix:Matrix = new Matrix();
                          // the offset of the gradient, which moves its center, is determined by the mouse position
                          matrix.createGradientBox(200, 200, 0, stage.mouseX-100, stage.mouseY-100);
                          graphics.clear();
                          graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix);//, "reflect"
                          graphics.lineTo(stage.stageWidth, 0);
                          graphics.lineTo(stage.stageWidth, stage.stageHeight);
                          graphics.lineTo(0, stage.stageHeight);
                          graphics.lineTo(0, 0);
                          graphics.endFill();
                  }
  
                  
Handler for when the mouse is moved. This redraws the gradient.
parameter: event The MouseEvent caused by the movement.

  
                  private function onStageMouseMove(event:MouseEvent):void {
                          drawBackground();
                          event.updateAfterEvent();
                  }
  
          }
  
  }
  


(C) Æliens 27/08/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.