Tutorial :Loading external SWF, cannot cast document class to shared base class


I have a parent SWF file that defines a Widget base class.

I then load an external SWF into the parent. The external SWF's document class derives from the Widget base class -- let's call it DerivedWidget for example.

The problem is that when I load the external SWF, I cannot cast the Loader.content (shows in debugger as having the DerivedWidget type) to the Widget type. I use the "as" operator to cast the content as a Widget in the parent SWF once the load completes, but the cast results in "null".

This behavior is not expected, because the external SWF is loaded into the parent's application domain, and the Loader's content shows up in the debugger as DerivedWidget, which I know derives from Widget, but the cast to the base Widget class is failing.

Can anyone explain this unexpected behavior and offer a solution?

[Edit: looking back at an earlier code snapshot... this was working before, but now it isn't... and the loading and casting code hasn't changed]

[Edit2: actually, it seems to be failing when debugging in the Flash IDE. When run normally, everything loads into the correct ApplicationDomain (sameDomain=true); this is obviously a huge bug]


There are some bugs in the way that DocumentClass does inheritance.

http://bugs.adobe.com/jira/browse/FP-2999 (need to log in to adobe's bug tracker)

My recommendation is to take it off the stage and export it through the library instead, it will work from there. In fact my rule of thumb is "never use document classes."


I have experienced the similar problem. The solution are two fold.

  1. Use DerivedWidget(mc), instead of mc as DerivedWidget. There are some articles addressing this problem, you need to find it out.

  2. make sure the class DerivedWidget is accessible in both compilation environment. If you use Module or invovle different Flash domain, be more careful.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »