package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.display.LoaderInfo; import flash.display.Sprite; import flash.events.Event; import flash.geom.Point; import flash.net.URLRequest; /** * Loads two images and displays them side by side with space to the right for a third image. * This is a base class that can be used to perform operations that require two images. */ public class graphic_flex_image_effects_03_Flex_DualImageTest extends Sprite { private static const IMAGE_0:String = "graphic-flex-image-effects-assets-hydrant.jpg"; private static const IMAGE_1:String = "graphic-flex-image-effects-assets-fungus.jpg"; protected var _bitmap0:Bitmap; protected var _bitmap1:Bitmap; protected var _bitmapData0:BitmapData; protected var _bitmapData1:BitmapData; /** * Constructor. Intiates load of first image. */ public function graphic_flex_image_effects_03_Flex_DualImageTest() { loadImage(IMAGE_0); } /** * Loads the specified image. * * @param imagePath The path to the image to load. */ private function loadImage(imagePath:String):void { var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded); loader.load(new URLRequest(imagePath)); } /** * Abstract method that is called once both images load. * This should be overridden by child class. */ protected function operateOnImages():void {} /** * Handler for when an image loads. This either kicks of load of the second image * or calls operateOnImages() when both images have fully loaded. */ private function onImageLoaded(event:Event):void { var loaderInfo:LoaderInfo = event.target as LoaderInfo; var bitmap:Bitmap = loaderInfo.content as Bitmap; // add the image that just loaded to the stage addChild(bitmap); // if this is the first image, start loading the second image if (numChildren == 1) { _bitmap0 = bitmap; _bitmapData0 = bitmap.bitmapData; loadImage(IMAGE_1); // if this is the second image, position it to the right of the first image // and call operateOnImages() } else { _bitmap1 = bitmap; _bitmapData1 = bitmap.bitmapData; bitmap.x = _bitmap0.width; operateOnImages(); } } } }