views:

56

answers:

1
+1  Q: 

IEventDispatcher

This piece of code is from Adobe docs:

package {
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.URLRequest;

    public class LoaderExample extends Sprite {
        private var url:String = "Image.gif";

        public function LoaderExample() {
            var loader:Loader = new Loader();
            configureListeners(loader.contentLoaderInfo);
            loader.addEventListener(MouseEvent.CLICK, clickHandler);

            var request:URLRequest = new URLRequest(url);
            loader.load(request);

            addChild(loader);
        }

        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(Event.INIT, initHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);
        }

 private function completeHandler(event:Event):void {
            trace("completeHandler: " + event);
        }
//...
}

Can anyone tell me why loader.contenLoaderInfo (passed as an argument to configureListeners) is an IEventDispatcher (and not a LoaderInfo) object?

+1  A: 

LoaderInfo extends EventDispatcher which in turn implements the interface IEventDispatcher. I don't think there's any reason for having it like that except that it's the most generic way to send an event dispatcher around.

This means that you could change the function to accept a LoaderInfo instead and it will work exactly the same, but not be as generic if you ever want to use that very function for something else that dispatches events.

grapefrukt