topical media & game development

talk show tell print

#graphic-flex-image-effects-02-Flex-ImageFilterTest.ax

#graphic-flex-image-effects-02-Flex-ImageFilterTest.ax [swf] [flash] flex


  package {
  
          import flash.display.Bitmap;
          import flash.display.Loader;
          import flash.display.LoaderInfo;
          import flash.display.Sprite;
          import flash.events.Event;
          import flash.net.URLRequest;
  
          
Abstract base class for bitmap filter tests requiring loading of an image.

  
          public class @ax-graphic-flex-image-effects-02-Flex-ImageFilterTest extends Sprite {
  
                  protected var _bitmap:Bitmap;
  
                  
Constructor. Initiates load of image.
parameter: path The path to the image to load. If no path is provided, a default image is used.

  
                  public function @ax-graphic-flex-image-effects-02-Flex-ImageFilterTest(path:String=null) {
                          loadImage(path || "graphic-flex-image-effects-02-assets-goat.jpg");
                  }
  
                  
Loads an image.
parameter: imagePath The path to the image to load.

  
                  private function loadImage(imagePath:String):void {
                          var loader:Loader = new Loader();
                          // just checks for success; a more robust class should also check for failures
                          loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
                          loader.load(new URLRequest(imagePath));
                  }
  
                  
Handler for when image loads successfully. This centers the bitmap on the stage and calls the applyFilter() method.
parameter: event Event dispatched by LoaderInfo.

  
                  private function onImageLoaded(event:Event):void {
                          var loaderInfo:LoaderInfo = event.target as LoaderInfo;
                          loaderInfo.removeEventListener(Event.COMPLETE, onImageLoaded);
                          _bitmap = loaderInfo.content as Bitmap;
                          _bitmap.x = stage.stageWidth/2 - _bitmap.width/2;
                          _bitmap.y = stage.stageHeight/2 - _bitmap.height/2;
                          addChild(_bitmap);
                          applyFilter();
                  }
  
                  
Abstract method to be overridden by child classes in order to apply a bitmap filter to the loaded bitmap.

  
                  protected function applyFilter():void {}
  
          }
  
  }
  


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