It’s quite common situation when you have to wait until couple of events will be dispatched before you can do “something that makes user to say wow”. I always used to use some flags variables to watch if all objects suspected to dispatch an event already did that but it was very confusing solution. So… I’ve just written this quite simple class to make that waiting much simpler.
To use it, all you have to do is something like this:
function foo ( e:Event ) { trace ( "done..." ); } var mel:MultipleEventListener; mel = new MultipleEventListener ( SomeEvent.EVENT ); mel.addEventListener ( SomeEvent.EVENT, foo ); for ( var i:int; i < someList.length; i++ ) { var obj:EventDispatcher = EventDispatcher ( someList[i] ); mel.addDispatcherObject ( obj ); obj.doSomethingAmazingAndThenDispatchEvent(); } mel.init();
The class stores the information if an object already fired an event or not (it uses internal EventStatus class) so it may happen that just after init function MEL dispatches the event. That happens when all object has already fired their events before you used init function so you have to be sure that you listen to MELs event before you initiate it. But I hope it’s obvious for you. ;)
Download MultipleEventListener class
maliboo
22/08/07 at 9:52 am
It’s better to use Dictionary class instead of Array. With such hashtable you can omit overcomplicated addDispatcherObject implementation ;)
Great blog BTW: keep on going!
Kisiaczki:*