Custom AS3 Loader class that uses a custom LoaderInfo class

actionscript-3

This is a question specifically about creating a custom Loader class as a followup to How do I modify existing AS3 events so that I can pass data?. If this wasn't the right way to go about this, let me know.

I have a custom Loader class and a custom LoaderInfo class. What I can't figure out is how to have the Loader class use the custom LoaderInfo class as it's contentLoaderInfo property.

package com.display {
    import flash.display.Loader;

    import com.display.CustomLoaderInfo;

    public class CustomLoader extends Loader {
        public var customData:*;

        public function CustomLoader(passedData: *= null) {
            customData = passedData;
        }
    }
}

^ Have to do something in there to make it have the new CustomLoaderInfo

package com.display {
    import flash.display.LoaderInfo;    
    import flash.events.Event;

    import com.events.CustomEvent;

    public class CustomLoaderInfo extends LoaderInfo {
        public var customData:*;

        public function CustomLoaderInfo(passedData: *= null) {
            customData = passedData;
        }       

        override public function dispatchEvent(event:Event):Boolean {
            var customEvent:CustomEvent = new CustomEvent(event.type, customData, event.bubbles, event.cancelable);
            return super.dispatchEvent(customEvent);
        }
    }
}

^ That might work, but since I can't get CustomLoader to use it, I don't know yet.

Best Solution

I don't know how it fits into what you're doing, but you could not bother making an extended LoaderInfo at all. Loader.contentLoaderInfo has a property called loader that will return the Loader (or in this case CustomLoader) that was used. In theory you can just do this with the CustomLoader class that you have:


var loader: CustomLoader = new CustomLoader("Extra Data");
loader.load(new URLRequest("file.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);

function loaderComplete(event: Event) : void
{
    var customData:* = (event.target.loader as CustomLoader).customData;
    trace(customData); // Extra Data
}
Related Question