0

ActionScript does not seem to support loading a png synchronously. I have tried loading it asynchronously, but then there is the problem of making your function wait for the load to finish. I cannot accomplish this. I have tried calling both setInterval and setTimout in a while loop, and breaking when the load is complete, but I get some very mysterious behavior. For the function parameter, I pass a function called doNothing that is just an empty function. When I run, setInterval or setTimeout gets over and over, very quickly. They do not wait the time provided before calling again. Also, doNothing never gets called.

Here's my code:

private function getData(extension:String):Bitmap
{
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onComplete);
    loader.contentLoaderInfo.addEventListener(ErrorEvent.ERROR, onComplete);
    loader.load(new URLRequest(baseDir + extension));

    while (bitmap == null)
    {
        var test:Number = setInterval(doNothing, 1000);
    }

    var ret:Bitmap = bitmap;
    bitmap = null;

    return ret;
}

function onComplete(event:Event):void
{
    bitmap = Bitmap(LoaderInfo(event.target).content);
}

function doNothing():void
{
    trace("did nothing");
}
  • The file loading protocols should have an event you can bind to so you will be notified when it is fully loaded. What are you doing? Code please? – TheZ Jul 06 '12 at 19:09
  • As Flex uses a single threaded model, trying to achieve something like that will lead the interface to freeze during the image loading process So the code calling the getData function should listen an even instead of directly expecting the data. Like TheZ said, what are you trying to do ? – cporte Jul 06 '12 at 19:42
  • Yes, that's actually what I need. – user1507613 Jul 06 '12 at 19:44
  • Well, If you really need that, you can look here : http://stackoverflow.com/questions/3009929/action-script-sleep-function But there is a risk that the onComplete function will not be called. Another solution would be to use the "callLater" function – cporte Jul 06 '12 at 19:54

1 Answers1

0

while loop is not good here since it will cause your client to freeze (and 15 seconds of freezing will throw an error)

Suggested Implementation:

private var loader:Loader;
private var onSuccess:Function;

// onSuccess = function (bitmap:Bitmap) : void
// onFail = function (e:ErrorEvent) : void
private function getData(extension:String, onSuccess:Function, onFail:Function):void
{
    this.onSucess = onSuccess;
    loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onFail);
    loader.contentLoaderInfo.addEventListener(ErrorEvent.ERROR, onFail);
    loader.load(new URLRequest(baseDir + extension));
}

private function onComplete(event:Event):void
{
    bitmap = Bitmap(LoaderInfo(event.target).content);
    onSuccess.call(null, bitmap);
    dispose();
}

private function dispose():void
{
    loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
    loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onFail);
    loader.contentLoaderInfo.removeEventListener(ErrorEvent.ERROR, onFail);
    this.loader = null;
    this.onSuccess = null;
}