Let's say I have some code like this in AS3
for(...)
thing = new Thing(...)
The constructor for Thing is going to cause a swf to be rendered onto some movieclip. If the loop is run 10 times or so, most of the time I will only end up with a subset of Things showing up on the clip. I'm guessing this is a problem that flash is having loading a bunch of Things at the same time.
I've been able to solve this problem by passing callbacks to the Thing constructor, and only continuing the loop once the previous Thing is completely loaded and calls back.
First of all, is this a known problem? Or am I going about this in the wrong way? Secondly, is there any way I can tell flash/as to run this code in a linear fashion without the callbacks? It gets kind of annoying doing this everywhere.
EDIT:
So I think richardolsson is right, we must be doing something wrong. To restate the problem, if I try to load multiple swfs onto the stage, even as few as two or three, without waiting for the previous one to finish loading, I will get an unpredictalbe number loaded. For example, running the loop above 10 times, I may end up getting the 2nd, 5th and 10th Things loaded. The others seemingly getting lost in asynchronousity. Flash shows no runtime errors in these situations. So here's the basic idea of how we are doing things.
We have a base class that we extend for most classes that load swfs. It looks something like:
public function base(path:string, parent:sprite, callback:function)
{
//this is the child swf path
_path = path;
//parent is the sprtie we want to render the child onto
if(parent != null) doLoad(parent, callback)
}
public function doLoad(parent, callback)
{
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest(_path);
//onLoaded is overridden in the child. The override first calls the parent onLoad.
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
mLoader.load(mRequest);
}
//this is called from the load complete from above. This is done before the
//child onloaded is done
protected function onLoaded(loadEvent:Event):void
{
_loadedTarget = loadEvent.currentTarget;
_canvas = _loadedTarget.content;
_parentSprite.addChild(_canvas);
}
Now, most of our classes which need to show a Swf extend this class. They look like this
public class child extends base
{
public function child(parent:Sprite, callback)
{
//call base constructor
super("some/path/to/swf", parent, callback);
}
protected override function onLoaded(loadEvent:Event):void
{
super.onLoaded(loadEvent);
....
}
This is the basic scheme. If I try to create a bunch of instances of 'child' WITHOUT waiting for the onLoaded function to finish on the previous 'child', random instances simply won't show up. I wrote the above code very quickly, it is not compy/paste, so ignore simple errors. Are there any glaring reasons why the above method does not work?