Necuima 18 Posted July 23, 2013 Report Share Posted July 23, 2013 I am wondering if someone can tell me how to force a wait till an asynchronous process finishes. Following is the code that I'm having trouble with: private function stage_1_processing(): void { var inStream:FileStream = new FileStream(); inStream.open(filein, FileMode.READ); var ba:ByteArray = new ByteArray(); inStream.readBytes(ba); inStream.close(); //Transform byteArray to bitmapdata loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, stage_2_processing); loader.loadBytes(ba); // this is an asynchronous operation!! } private function stage_2_processing(evt:Event): void { trace("Entering stage 2 processing"); var content:* = loader.content; var bigBMD:BitmapData = new BitmapData(content.width,content.height,true,0x00000000); bigBMD.draw(content,null,null,null,null,true); ...... I want to force the program to wait until the loader.loadBytes process is finished before continuing on to the stage_2_processing function. It seems to 'fall through' to the stage_2_processing logic before the asynchronous loader.loadBytes process is finished. Any assistance/guidance will be much appreciated. Cheers from Oz. Quote Link to post Share on other sites
HartleySan 826 Posted July 23, 2013 Report Share Posted July 23, 2013 The following API reference may help: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#loadBytes() It says something about having to wait for the "init" event. You might want to look into that. Quote Link to post Share on other sites
Necuima 18 Posted July 23, 2013 Author Report Share Posted July 23, 2013 Hi HartleySan, Yes, thanks for the tip. What was confusing me was that I had three 'alert' displays in the stage_2_processing function and they were displaying in the reverse order than I was expecting! I seem to recall that I had come across this once before and was never able to figure out why the alerts were displaying in the reverse order. But display in the reverse order they do, but the code actually executes in the correct, sequential order! In the end I discovered that the 'bug' in the function was unrelated to the asynchronicity of the loader.loadBytes - embarrassingly I had a simple spelling error!! Many thanks for your assistance. Cheers from Oz. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.