0

So I have been trying to do something like the following in AS3 to load any image and retrieve its bitmap data during runtime:

var bitmapData:BitmapData;
var loader:Loader;
var completed:Boolean = false;

public function loadBitData(path:String):BitmapData{
    loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    loader.load(new URLRequest(path));
    while(!completed){

    }
    completed = false;
    return bitmapData;
}


private function onComplete (event:Event):void{
    bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
    completed = true;
}

Since the loader will not load the file on a separate thread, this code will obviously get stuck in an infinite loop. If you remove the while loop the method will return null because by the time it reaches that statement the data has not been loaded. It will still load the data afterwards however. (I have checked this using a progress event and printing the amount of data that has been loaded so far)

I just want the method to load the file and then return the BitmapData once it has been fully loaded. The program should wait and not execute any more code until this happens. Any help on this would be greatly appreciated.

Jeroen
  • 13,056
  • 4
  • 42
  • 63
user1434120
  • 3
  • 1
  • 2
  • You can't turn an asynchronous function into a synchronous function. Have the code you want to run after the data is loaded inside the complete event. – The_asMan Jun 04 '12 at 02:30

2 Answers2

1

No, the program should not wait until the image has loaded. Loading data is done asynchronously.

In programming, asynchronous events are those occurring independently of the main program flow. Asynchronous actions are actions executed in a non-blocking scheme, allowing the main program flow to continue processing.

Your application will continue to run normally while external data is being loaded. Your onComplete() method is where you need to manage what should happen once that data is fully loaded. If you don't want something to happen until that point, move that code into the onComplete handler.

Marty
  • 39,033
  • 19
  • 93
  • 162
  • But what if the thing that i don't want to happen until the loader completes is the entire rest of the program? I'm trying to use the loader class in a program that previously did not use it. I would rather NOT refactor the entire thing just so that it can work with this class. – android927 Jan 19 '15 at 05:03
  • @android927 That is a pretty standard situation, and you would do exactly that - change your application so that it begins after all the dependencies have loaded (or failed to load). – Marty Jan 19 '15 at 05:05
  • I was hoping to keep the structure of the program intact and let each object load it's own assets, but then the things that rely on those objects to have a valid non-null DisplayObject break because it hasn't finished loading yet. Waiting for loading to finish before continuing is standard behavior in every other API i've used (XNA, Slick2D). Why should this functionality not be available in actionscript? – android927 Jan 19 '15 at 05:09
  • @android927 if you're talking about game assets like XNA's `Content.Load` then you can use the `[Embed]` syntax in AS3, which embeds those assets into the SWF at compile-time. You can [read more about that in this thread](http://stackoverflow.com/questions/14549136/as3-embed-images-class-and-then-get-these-images-into-another-class). – Marty Jan 19 '15 at 05:12
  • Does this work with all file types? I need the program to have access to text files (txt), image files (png and maybe svg), and sound files (probably mp3 or ogg). Will this work with all of these file types? – android927 Jan 19 '15 at 05:16
  • @android927 Yes, as far as I am aware it supports those file types. You also have the option to provide the `mimeType` in an `[Embed]` block. – Marty Jan 19 '15 at 05:18
  • This method doesn't work for variable strings, so i still am unable to do stuff like loading in multiple tilemap files without having to change the code. Is there really no true equivalent to Content.Load? I really don't feel like putting onComplete listeners on every single method that relies on having access to the tilemap files just because actionscript doesn't offer this very basic functionality. – android927 Jan 20 '15 at 05:19
  • @android927 Are you trying to load a `.txt` file, is that the problem? – Marty Jan 20 '15 at 05:20
  • Well i was thinking that the tilemaps would take the form of a raw text file, but with a .map file extension. The problem is that the program would not be able to construct the tilemap until the map file was loaded, and then the parts of the game that rely on the tilemap would not function until the tilemap is constructed, and this chain of dependencies continues on and on. I really just need the program to stop and wait for everything to be loaded, but then this will prevent the file from loading (like the while loop example in the OP). – android927 Jan 20 '15 at 05:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69168/discussion-between-marty-and-android927). – Marty Jan 20 '15 at 05:32
0
  1. If you will use LoaderMax - just after starting a load, you will get a special display object with fixed size, that will automatically update when proper image will load. That may solve your problem if you need to have a place on stage with size of the image, before it actually loaded.

  2. If you really need to handle this without third party tools or you need it for something different, just dispatch a custom event when data will load, and then let your application react. You cannot handle this with only that one function. This is asynchronous thing so you need to have proper architecture. Try to redesign things around events.

Łukasz Zaroda
  • 869
  • 2
  • 19
  • 55
  • 1
    Sure, he can use it also, it depends on architecture of the whole application. I just used to using custom events with data attached :) . – Łukasz Zaroda Jun 03 '12 at 23:51
  • True, having your own event does give the advantage of adding `BitmapData` as a property of the event, eliminating a step in the COMPLETE handler. – Marty Jun 03 '12 at 23:52