topical media & game development

talk show tell print

#graphic-flex-image-effects-05-Flex-AnimatedSquareGradientTest.ax

#graphic-flex-image-effects-05-Flex-AnimatedSquareGradientTest.ax [swf] [flash] flex


  package {
  
          import flash.events.Event;
  
          [SWF(width=300, height=300, backgroundColor=0x000000)]
  
          
Builds on SquareGradientTest to show how a shader can be animated by altering its properties over time.

  
          public class @ax-graphic-flex-image-effects-05-Flex-AnimatedSquareGradientTest extends graphic_flex_image_effects_05_Flex_SquareGradientTest {
  
                  private static const ROTATION_RATE:Number = 5;
  
                  private var _rotation:Number;
  
                  
Constructor. Initializes properties.

  
                  public function @ax-graphic-flex-image-effects-05-Flex-AnimatedSquareGradientTest() {
                          _rotation = 0;
                  }
  
                  
Updates the shader with new property settings.

  
                  private function updateShader():void {
                          _rotation += ROTATION_RATE;
                          // keeps rotation between 0 and 360 degrees
                          if (_rotation >= 360) {
                                  _rotation = 0;
                          }
                          _shaderProxy.rotation = _rotation;
                          _shaderProxy.center = [stage.mouseX, stage.mouseY];
                          // redraws graphic with updated shader
                          graphics.clear();
                          graphics.beginShaderFill(_shaderProxy.shader);
                          graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
                          graphics.endFill();
                  }
  
                  
Draws with shader initially. This override of super class method also sets up a listener for the ENTER_FRAME event so that the shader can be animated.

  
                  override protected function drawShader():void {
                          super.drawShader();
                          addEventListener(Event.ENTER_FRAME, onSpriteEnterFrame);
                  }
  
                  
Handler for the ENTER_FRAME event, updating the shader with new settings.
parameter: event Event dispatched by this sprite.

  
                  private function onSpriteEnterFrame(event:Event):void {
                          updateShader();
                  }
  
          }
  
  }
  


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