Question

I want to show several images to my application

            sourceAudioFile =(event.target as File);
            sourceAudioFile.data;

            var myLoader:Loader = new Loader(); 
            var url :URLRequest = new URLRequest("file:///" + sourceAudioFile.nativePath);
            myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
            myLoader.load(url); 

Registration Listener

private function onImageLoaded(e:Event):void 
{
            var image : Bitmap= new Bitmap(e.target.content.bitmapData);
            image.scaleX = 0.5 ; 
            image.scaleY = 0.5 ; 
}

Is there any way by which i can add some custom values to Event.COMPLETE to know what type of image is being loaded.

like e.imageName , e.noOfImage which i can access in complete handler & provide values to it when i register this event.

Thanks in advance

Was it helpful?

Solution

Yes you can, try registering event like:

myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event){

  dispatchEvent(new CustomEvent(CustomEvent.COMPLETE, "<imageType>"));

}, false, 0, true);

And Add new Class, extending the Event class:

public class CustomEvent extends Event {

        public static const COMPLETE:String  = "CustomEventComplete";

        public var imageType:String = "";

        public function CustomEvent(type:String, imageType:String = "", bubbles:Boolean = false, cancelable:Boolean = false){

            super(type, bubbles, cancelable);

            this.imageType = imageType;
        }

        override public function clone():Event {

            return new CustomEvent(type, customTarget, bubbles, cancelable);
        }

    }

Now you can register for CustomEvent.COMPLETE & get the image type in the listener.

OTHER TIPS

You can find out what ahs been loaded by checking LoaderInfo's url property

private function onImageLoaded(e:Event):void 
{
var cli:LoaderInfo = e.target as LoaderInfo;//you added listener to the contentLoaderInfo property
    trace(cli.url);//url of the media being loaded
    //do something with it
}

also in this SO question you can get more sophisticated asnwer to your problem:)

pass arguments to event listener

Yes, there's a simple way:

myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded(imageName, noOfImage));
myLoader.load(url);

function onImageLoaded(imageName:String, noOfImage:int):Function {
  return function(e:Event):void {
    // Respective "e", "imageName" and "noOfImage" are available for you here.
  }
}

If you need to remove it later, change the first line to:

var functionOnImageLoaded:Function = onImageLoaded(imageName, noOfImage);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, functionOnImageLoaded);

Then later just do it like this:

myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, functionOnImageLoaded);

I've answered this before. See it here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top