AS3 Embedded MovieClip Gotcha

In AS3 you can load an asset from a SWF using the Embed metatag like this:

[as][Embed(source=”Assets.swf”, symbol=”MySymbol”)]
var mcClass:Class;
var myMC:MovieClip = new mcClass();
addChild(myMC);[/as]

Where ‘Assets.swf’ is a swf that contains a symbol with linkage class name ‘MySymbol’. Sometimes when you do this you will get the following error:

TypeError: Error #1034: Type Coercion failed: cannot convert MyClass_mcClass@12510d1 to flash.display.MovieClip.

I finally figured out why: Flash will type the symbol based on the number of frames in it’s timeline. If the symbol has only 1 frame it will be typed as a Sprite, 2 or more frames it will be typed as a MovieClip.

So to fix the above error, just add another frame in your symbol’s timeline. Of course you could type your object as a sprite instead of a movieclip, but there are reasons why you may not want to (for example accessing sub-movieclips, adding dynamic properties etc).

37 Responses

  1. tamt says:

    thanks~!

  2. lucas says:

    this was killing me the other day – thanks for the solution.

  3. N. says:

    Cant you type cast it ?

    var myMC:MovieClip = new mcClass() as MovieClip;

    or

    var myMC:MovieClip = MovieClip( new mcClass() );

  4. Felix Turner says:

    @N: Both those methods of typecasting give a runtime error. Either the same one as above or “TypeError: Error #2007: Parameter child must be non-null.”

  5. Patrick Matte says:

    Does this embed thing also work in flash cs3 or is that Flex only?

  6. Geoff says:

    Thanks Felix! You are my hero!

  7. Clyde says:

    Thanx alot dude, don’t think I would’ve figured that one out on my own.

  8. Bob says:

    Thanks Felix . I just faced this issue and your blog answered it. Saved me a lot of time.

  9. r says:

    brilliant!

  10. nise says:

    hm… that sniped doesn’t work with Flex 3.

    But I found out that the following works.

    [Bindable] [Embed(“../myLib.swf#screen_help”)] var lib:Class;
    ar button:Button = new Button();
    button.label = “Star”;
    button.setStyle(“icon”, lib);/* where the snd arg requires an Object */
    addChild(button);

    unfortunatly I have no idea to transfer it into a broader use case to match MovieClips.

  11. wessidemd says:

    DUDE! You just saved my life with this…. This is the only place where this INTENDED DESIGN FLAW is documented! I absolutely hate it when Actionscript3 decides to do me these kind of “favors”.

    Way to go once again!

  12. DJC says:

    O. M. G. I owe you a beer 🙂

  13. sebastienk says:

    thanks a lot, I was blocked for two days , you set me free 😉

  14. Brian Davis says:

    Patrick Matte: no one answered your question, here is the answer.
    Just Flex
    here is a resource about it:
    http://frankieloscavio.blogspot.com/2007/12/embed-meta-tag-is-flex-only-it-will-not.html

    and its a shame too, i had a few neat plans for using this as a way of working with external assets. working with MochiMedia it is best if everything is all contained in one swf. This would have allowed the artist working on this with me to manage assets the easier external way. Oh well….

    what makes it even more of a shame is that xml has this ability with the Embed method too… as documented about its undocumented ness here
    http://dispatchevent.org/roger/embed-almost-anything-in-your-swf/

    well its back to being cooped up in one fla, for mochi.

    (I am aware mochi media lets you zip files and send a game package across but there are several nice features that mochi offers that don’t support their zip support.)

  15. Mike Britton says:

    You should put a paypal donation link up here because this has been bothering me for some time. I didn’t think adding keyframes would solve it. Cheers.

  16. YES! Finally, been racking my head on this one for a day or so. Just want to say thanks for pointing out the problem. And if any of you guys from Adobe are listening, it should be based on the base class defined not the # frames.

  17. […] element to become parent of the imported MovieClip symbol. Here comes the first major problem. As http://www.airtightinteractive.com describes this technique does not work every time: Sometimes when you do this you will get the […]

  18. Ty says:

    man alive. Thank you so much for the Sprite/MovieClip error explanation. I was ripping out hair.

  19. Swastik says:

    thanks mate…really saved my skin

  20. Thorben Schmitt says:

    Great! Thank you!

  21. Adriaan says:

    This was a lifeseaver, like the above ppl in the comments I was banging my head against the wall screaming why, Why, WHY!? Just about on the verge of giving up my life as a Flash developer and starting a paper route when I encountered your nugget of wisdom. So thanks 🙂

  22. mike says:

    phew… so glad I found this. I was on verge of slitting my own throat

  23. simon says:

    Cool…

    I spent over 5 hours trying to figure out what I did wrong was going nuts.
    You are a lifeseaver.

  24. mirko says:

    Thank you! I was going really angry on it. 🙂

  25. Unfortunately the solution didn’t solve my problem. It seemed to me my 1 frame assets should be safely castable to MovieClips as long as it’s not the other way around.
    My solution might be useful to others, so I’ll post it here: FDT (and Flash Builder) use FCSH for caching. The compiling happens in MXMLC. This caching is the problem.
    In my case I needed to republish my assets swf every time before compile, else the coercion error would occur.
    But resetFCSH solves this. There’s a button in the compile settings, but making it into a precompile ant-task is the perfect solution for me.

  26. Cecile says:

    So simple & and so useful! Thank you so much for this valuable tip!!

  27. e1 says:

    thank you. I was losing hours trying to figure out why typecast stopped working. Then I saw this and remembered I’ve deleted extra frames…

    Tnx!

  28. Ken says:

    Very helpful – this one was killing me too. Thanks.

  29. Ombossa says:

    Very useful thanks! It was driving me crazy

  30. Jeremy says:

    Thanks man. Was driving me crazy for an hour trying to figure this one out. Fortunately I googled it and found this page. Phew!

  31. andreas says:

    thanks a bunch! crazy adobe

  32. Scott says:

    The only difference between a Movieclip and a Sprite is the timeline. You still have all other functionality that remains the same, including accessing child objects (no matter what type they are) accessing dynamic properties, etc. The only thing you are doing by adding a second unnecessary frame is adding overhead to your swf.

  33. YopSolo says:

    Thx a lot for this info !

  34. Simanas says:

    Thanks mate! I was pulling my hairs out of my head by trying to figure out what am I doing wrong… 🙂

Leave a Reply to nisse Cancel reply

Your email address will not be published. Required fields are marked *