Image Class with ActionScript3
17/01/2009
In Flex is easy to have components already programmed and save a lot of time in coding.
Such as the Image component.
Flex Image component for what it does is give us the ability to load an image very easily, but and flash? so i develop a class that fulfills the same function as the Image class of Flex.
Let's see how it works:
import phoxer.Images.Imagen; var img:Imagen = new Imagen("http://www.phoxer.com/images/phoxer.jpg",onImageProgress,onImageLoaded); addChild(img) function onImageLoaded(bm:Bitmap,ld:Loader):void{ //bm: Bitmap is the object of the loaded image. //ld: Is the Loader object to load the image. } function onImageProgress(num:int):void{ //num: is the number 0 to 100 the progress of the load. }
The advantage we have with this is that we forget to create a Loader for each image you want to include in our projects
include images in this way is much easier and faster.
Here is my class picture Imagen:
/** by .:[PHOXER]:. http://www.phoxer.com v 1.5; */ package phoxer.Images{ import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.IEventDispatcher; import flash.events.ProgressEvent; import flash.net.URLRequest; public class Imagen extends Sprite{ public var loader:Loader; public var id:String; private var fileurl:URLRequest; private var backProgress:Function=null; private var backComplete:Function=null; public function Imagen(url:String,prg:Function=null,cmp:Function=null){ super(); fileurl = new URLRequest(url); loader = new Loader(); loader.name="loader"; loader.load(fileurl); backProgress=prg; backComplete=cmp; listeners(loader.contentLoaderInfo); this.addChild(loader); } public function getWidth():Number{ return loader.width; } public function getHeight():Number{ return loader.height; } private function listeners(e:IEventDispatcher):void { if (backProgress!=null) { e.addEventListener(ProgressEvent.PROGRESS, onProgress,false,0,true); } if(backComplete!=null){ e.addEventListener(Event.COMPLETE, onComplete,false,0,true); } } private function onProgress(e:ProgressEvent):void { var total:Number = Math.round((e.bytesLoaded*100)/e.bytesTotal); backProgress(total); } private function onComplete(e:Event):void { backComplete(loader.getChildAt(0),loader); } } }
