views:

896

answers:

4

Hi there,

Currently I'm working on an application (Flex) which heavily uses external SWFs. I'd like to create a static method which takes a filename as an argument and returns SWF wrapped in some other class.

public static function getSWFWrapperFromFile(path:string):SWFWrapper {
   var loader:SWFLoader = new SWFLoader();
   loader.addListener(Event.COMPLETE, onLoad);
   loader.load(path);
   // If I create new SWFWrapper object here and try to assign it the loader.content  I get null reference

  }

However, with this approach I'm not able to encapsulate logic in one function, because of non-blocking load() and the need of onLoad handler. Is there possibility to force a delay after calling load method? Or mayber there is another, better way?

Thank you, Alonzo

A: 

You need to wait until your Loader object has completed. Try adding in an event handler. Yes, the whole thing gets murky after a point of time when you have multiple loaders and and have to wait till the last one has completed. But that's the way it is if you are going to use SWFLoader.

dirkgently
A: 

In flash you cannot block in a method - you always have to use the onLoad handler for loading data. (The as3 execution model is single threaded, if you block in a method the rest of the program will not get executed)

Simon Groenewolt
A: 

Like others said, you can't do that. However, take a look at the BulkLoader AS3 library, which takes the burden of managing multiple loaders simultaneously and waiting for their completion, off your shoulder. It is well documented, and requires only a few lines to use.

David Hanak
+1  A: 

The display list is well-designed for asynchronous loading. You'll notice that Loader is a DisplayObject-derived class and thus can be placed directly in the display list. When its content is loaded it will be a child of the Loader. Thus, if SWFWrapper is DisplayObject-derived, you can simply do the following at the end of your code:


var wrapper:SWFWrapper = new SWFWrapper();
wrapper.addChild(loader);
return wrapper;
Troy Gilbert