2

I am struggling passing paramters to function on onComplete event handler.

It seems that my problem is with the event.Complete code..

I just want to load image from a url and transfer parameter.

This is my code:

var imageURLRequest:URLRequest = new URLRequest(pic); 
var myImageLoader:Loader = new Loader(); 
myImageLoader.load(imageURLRequest); 

myImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(evt:Event.COMPLETE){
    doIt(evt, "Shift key was down:")
},false,0, true);

function doIt(evt:Event, msg:String) {
    var myBitmapData:BitmapData = new BitmapData(myImageLoader.width, myImageLoader.height); 
    myBitmapData.draw(myImageLoader); 
    var myBitmap:Bitmap = new Bitmap; 
    myBitmap.bitmapData = myBitmapData; 
}

Thank you very much.

Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
Yan
  • 1,424
  • 4
  • 21
  • 44

3 Answers3

2

Remove .COMPLETE from the handler inner function so that your listener looks like this:

myImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(evt:Event)
{
   doIt(evt, "Shift key was down:")
} , false, 0, true);
Gio
  • 1,954
  • 3
  • 22
  • 42
  • 1
    Try to remove that listener (from outside the closure), you can't. This could cause memory leaks, so I would recommend to never use this. Just store the data you need locally or see what event.target or event.currentarget (from a normal event handler) could give you. – Mark Knol Sep 11 '12 at 17:28
  • I agree with you on that, but in this case scenario you don't need that listener after closure. Also he just wanted to know why it didn't work. Although I'd also suggest Jevgenij's answer for better overall results and practices. That's why I voted it up. EDIT: Also he uses weak references here, so it shouldn't be a problem. – Gio Sep 11 '12 at 19:52
2

Look at the Loader class as loader, not as DisplayObject even when it is:

var myBitmap:Bitmap;
var contentLoader:Loader = new Loader();
contentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete);
contentLoader.load(imageURLRequest); 

function handleComplete(e:Event):void
{
    myBitmap = contentLoader.content as Bitmap;
}
Jevgenij Dmitrijev
  • 2,228
  • 1
  • 15
  • 23
1

Firstly, as Gio said, remove that .COMPLETE from evt:Event.COMPLETE because it's returning a String instead of the Event the function needs.

Then, instead of setting that last fearsomely unpredictable parameter (useWeakReference) to true in your addEventListener(), I recommend you keep the reference in a variable to use removeEventListener() on it at the right time. A way to do this (while answering your question) is:

var imageURLRequest:URLRequest = new URLRequest(pic);
var myImageLoader:Loader = new Loader();
myImageLoader.load(imageURLRequest);

var functionDoIt:Function = doIt("Shift key was down:");
myImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, functionDoIt);

function doIt(msg:String):Function {
  return function(evt:Event):void {
    // Now you can use both "msg" and "evt" here
    var myBitmapData:BitmapData = new BitmapData(myImageLoader.width, myImageLoader.height);
    myBitmapData.draw(myImageLoader);
    var myBitmap:Bitmap = new Bitmap(myBitmapData);
  }
}

// Remove the listener when you don't need it anymore:
//myImageLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, functionDoIt);

You can understand this solution better by reading this answer.

Community
  • 1
  • 1