Question

I have a problem with my assets loader I've created for my flash app. In a function I try to load an external image and return it from this function. My problem is that I always get loader.content set to null. This how my code looks like:

    public static function loadImage(path:String):BitmapData
    {
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, check);
        if (path == "")
            loader.load(new URLRequest("images/default.png"));
        else
            loader.load(new URLRequest("images/" + path));
        var image:Bitmap = new Bitmap((loader.content as BitmapData));
        return image.bitmapData;
    }

    public static function check(e:Event):void
    {
        trace("Loading completed");
        e.target.removeEventListener(Event.COMPLETE, check);
    }

Any ideas???? I've also included my event listener which does launch.

I'm pretty sure it's because the loader works asynchronously, but I have no idea how to modify it to be able to return the image from the function.

Thanks in advance!

Was it helpful?

Solution

I'm pretty sure it's because the loader works asynchronously

Correct

but I have no idea how to modify it to be able to return the image from the function

There is no way to do that.

Why?

Because it is asynchronous.

You can't return a result of a request straight after the request has been made.

The execution of the code does not pause and wait for the response to come back, so your function will hit the return statement before you get the data, and that is why it is returning null.

The only way to handle this is to listen for the response (Event.COMPLETE) and run a callback function after the response from the server has been received.

So following your code example, you could do:

private static var _callback :Function;

public static function loadImage(path:String, callback:Function):void
{
     _callback = callback;
     // ....

and then:

 public static function check(e:Event):void
 {
     // ...
     _callback( whateverYouWantToReturnGoesHere );

This should work assuming that you only have one image loaded at a time (as your methods are static), otherwise you will need to implement some sort of request queue handling.

I hope this helps.

OTHER TIPS

Your getting a 'null' because you're referring to loader.content too soon. Here's a simplified version that works, just fine, in a timeline:

 var loader:Loader = new Loader();
 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, check);

 loader.load(new URLRequest("images/default.png"));



function check(e:Event):void
{
    var bmd:BitmapData = new BitmapData(195,130,false,0xFFFFFFFF)
    var image:Bitmap = new Bitmap(bmd);
    bmd.draw(loader.content);
    trace(loader.content);
    trace("Loading completed");
    addChild(image);
    e.target.removeEventListener(Event.COMPLETE, check);
}

You can put it back into a form suitable for your classes.

You should say:

var bitmapData:BitmapData; 

and

var image:Bitmap = new Bitmap(event.target.content.bitmapData)

instead of

var image:Bitmap = new Bitmap((loader.content as BitmapData));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top