Type Coercion Failed of the Same Class?

January 14th, 2009 | by Matt |

So I have this issue that I just can't seem to figure out. The Flash Player keeps throwing a Type Coercion Failed error at runtime. Usually when this error pops up I have no problem figuring it out because its usually two different object types. However, in this case, this time its of the same type. It reads:

TypeError: Error #1034: Type Coercion failed: cannot convert super.secret.package::BeverageAsset@2f2b96a1 to super.secret.package.BeverageAsset.

Odd, right? Ok, so now let me explain the structure a bit. At the top level is app.swf. This SWF does not include the BeverageAsset class compiled into it. This SWF manages the loading and unloading of other SWF files that represent sections. One of these sections is recipes.swf. This SWF is loaded into app.swf and is the first SWF to have BeverageAsset compiled into it. This SWF also loads various SWF display assets in order to function properly. One of these SWF's is beverage01.swf. These assets are of type BeverageAsset. This class is used as the document class for each display asset which also has two movie clips on the stage.

So, when I compile recipes.swf and it runs standalone there is no type coercion failure. I only get the error when recipes.swf is loaded and run within app.swf. Now in my experience, I thought all child SWF's loaded into an app, unless explicitly told not to, would use app.swf's application domain unless a class does not exist yet. So once recipes.swf is loaded into app.swf, since BeverageAsset is compiled into recipes.swf, it would be added to the application domain. Thus, once beverage01.swf is loaded into recipes.swf the BeverageAsset class will already be in the application domain and that one will be used.

I mean, am I totally wrong? I'm totally baffled. Any help would be greatly appreciated.

UPDATE!

Thanks Steven, for pointing me in the right direction. My assumptions about the applicationDomain for any loaded SWF was entirely wrong. I really have no idea why I assumed everything was added to the system domain as SWF's are loaded into the app. So what I have done now is define the application domain of any Loader object's context.

var context:LoaderContext = new LoaderContext( false, ApplicationDomain.currentDomain );
loader.loader( request, context );

This has eliminated any type coercion errors when loading SWF's and has allowed me to refer to them as a strongly type object. Thanks again Steven. And, man, I really need a new code formatter.

  1. 14 Responses to “Type Coercion Failed of the Same Class?”

  2. If the BeverageAsset class existed in your app.swf then I think you would be correct – any child swfs would by default use this definition and you wouldn’t have the problem… So that is one solution if the class isn’t big…

    Or you could maybe have an interface in your app.swf that the BeverageAsset implemented and refer to your instances as IBeverageAsset instead of BeverageAsset.

    Or you could try setting the ApplicationDomain to ApplicationDomain.currentDomain when you load each child swf (see http://livedocs.adobe.com/flex/2/langref/flash/system/LoaderContext.html#applicationDomain ) and see if that helps…

    Kelvin Luck on Jan 14, 2009 | Reply

  3. I got same issue when AIR application load SWF files from ‘app-storage’ folder.

    George on Jan 14, 2009 | Reply

  4. Now in my experience, I thought all child SWF’s loaded into an app, unless explicitly told not to, would use app.swf’s application domain unless a class does not exist yet.

    I’m pretty sure that is backwards…child swfs use their own application domain unless explicitly told to do so by the loading swf.

    From the livedocs:

    When loading the SWF file, you can specify that the file be included in the same application domain as that of the Loader object, by setting the applicationDomain parameter of the LoaderContext object to ApplicationDomain.currentDomain.

    http://livedocs.adobe.com/flex/3/html/help.html?content=18_Client_System_Environment_5.html

    Rich Rodecker on Jan 14, 2009 | Reply

  5. Hi there. I just googled this issue and found your blog. I seem to be having the same problem, and I too, am totally baffled. If you hear about a fix, please let me know. I’ll do the same…

    Ryan Unger on Jan 14, 2009 | Reply

  6. http://blogs.adobe.com/rgonzalez/2006/06/applicationdomain.html

    Steven Sacks on Jan 15, 2009 | Reply

  7. Google: ApplicationDomain

    First link.

    Steven Sacks on Jan 15, 2009 | Reply

  8. wow, looks like I was off on that too…good to know.

    Rich Rodecker on Jan 15, 2009 | Reply

  9. Hi,
    I been having a tough time trying to find the answer to the following: I would like to pass an instance of a class in the loading swf to the loaded swf an I am getting conversion error #1034 and it is driving me nuts… I can pass a flash type variable like a String in or Array but no such luck with a Class I created. I read through the posting here and on Roger Gonzalez’s blog in effort to understand if I am doing some thing wrong with Application Domain or if simply can not do it..

    /**Loading class**/
    package..{
    import com.mashboard.obj.Item;

    private var _item:Item;
    private var wdgt:Object;
    private var myLoader:Loader;
    ….
    private function loadWidgetAction():void{
    ….
    var url:String = “http://……/WidgetActionStage.swf”;
    var urlReq:URLRequest = new URLRequest(url);
    var appDomain:ApplicationDomain = ApplicationDomain.currentDomain;
    var context:LoaderContext = new LoaderContext(false, appDomain);
    this.myLoader.load(urlReq,context);
    }
    private function onWidgetActionStageLoaded(e:Event):void{
    ….
    var was:Class = myloader.contentLoaderInfo.applicationDomain.getDefinition(“com.mashboard.view.assets.actionviews.WidgetActionStage”) as Class;
    this.wdgt = new was();
    //passing these type of parameters works fine….
    this.wdgtActnStgObj.setUp(width, height); //works fine…
    if(this._item){
    //TypeError: Error #1034: Type Coercion failed: cannot convert com.mashboard.obj::Item@13720821 to com.mashboard.obj.Item.
    this.wdgtActnStgObj.setItem(this._item); //ERRROR
    }
    }

    /**Loaded SWF**/
    package …{
    import com.mashboard.obj.Item;

    private var _item:Item;
    …..
    public function setItem(item:Item):void{
    this._item = item;
    }

    }

    Any help would be appreciated … if anyone can point me in the right direction or tell me if this is even possilbe… Thank you in advance….

    this.wdgtActnStgObj.setUp(width, height);
    if(this._item){
    this.wdgtActnStgObj.setItem(this._item);
    }

    Will Streeter on Jan 28, 2009 | Reply

  10. Thanks for pointing this out. We ran into this in Flex and had to remember to set myModuleLoaderInstance.applicationDomain = ApplicationDomain.currentDomain.

    Also, you’d asked about a code formatter–do you mean for your blog or eclipse? I’ve been liking this: http://sourceforge.net/projects/flexformatter/

    Francis Lukesh on Apr 7, 2009 | Reply

  11. If you’re loading from one domain to another, you may also need to include the SecurityDomain in the loader context.

    Alex on Apr 25, 2009 | Reply

  12. “If you’re loading from one domain to another, you may also need to include the SecurityDomain in the loader context.”

    Alex, I tried this and it doesn’t work. It seems as though when a swf is loaded from another domain it is ALWAYS a new Application Domain and you can’t convert a class instance in the loaded swf as the same Class.

    It is driving me mental. If anyone knows a solution please tell. I have also tried the ByteArray Class Serialization method and it fails when the swf is on another domain.

    The only solution is to declare the instance as an Object and ‘pretend’ the object as the Class instance.

    Rob on Jun 2, 2009 | Reply

  13. this is the third time u’ve saved me, dude. ha, u and i need to brainstorm more apparently, because this little tidbit of info would’ve give me 3 hours of my life back.

    faisal on Sep 2, 2009 | Reply

  14. +10 hours stuck with this error! I was going nuts and had absolutely no clue whatsoever on where to look.
    thanks!

    esko on Feb 26, 2010 | Reply

  15. I got same issue and I resolve it defining ApplicationDomain.
    Thanks a lot to all!

    Ufic on Jul 7, 2010 | Reply

Post a Comment