views:

484

answers:

2

I am dispatching events from a child to a parent swf. It was working properly, until I used a preloader swf to load the parent, then the parent stopped getting events from the child. I get this error now:

TypeError: Error #1034: Type Coercion failed: cannot convert com.company.events::MyCustomEvent@22494251 to com.company.events.MyCustomEvent.
    at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()

What does that mean? And why does it only happen when I wrap the main swf in a loader? Thanks for any help - I'm desperate!

+2  A: 

Sounds like an ApplicationDomain issue.

Try something like this:

public function loadSWF(url:String):void 
{
    var req:URLRequest = new URLRequest(url);
    var cxt:LoaderContext = new LoaderContext();
        cxt.applicationDomain = ApplicationDomain.currentDomain;

    var ldr:Loader = new Loader();
        ldr.load(req, cxt);
}
Coded Signal
Thank you so much. I'm using BulkLoader so I add to modify for that, but it works now. Can you tell me why? I can't wrap my head around this context/applicationDomain stuff. I was loading child swf's before just fine - why does event handling break when wrapping the main swf in a loader?
ApplicationDomains represent separate "worlds" of classes. When you load in an external SWF, typically its classes will all exist in a separate ApplicationDomain from the classes in the SWF loading it. This is what your original code was doing, and it led to the class named "MyCustomEvent" in your loaded SWF being considered a completely different class than "MyCustomEvent" in the loading SWF, because each class was in a different ApplicationDomain.CodedSignal's code makes the SWF's classes should be loaded into the same ApplicationDomains, so its MyCustomEvent isnt seen as a different class
IQpierce
You saved my day IQpierce! Thank you!!
Richards
A: 

why does event handling break when wrapping the main swf in a loader?

It doesn't really "break"... it's more that methods and properties are namespaced for each swf so that there isn't a conflict in the event that you load a random swf - say an advert - into your shell app and mayhem ensues because events in the child are being caught by the parent as they travel up the display list.

It is kinda voodoo though, so don't worry about finding it confusing. You can find a good overview here.

Coded Signal
thanks for the link.
well you can always upvote if you like... ;)
Coded Signal