lang="en-US"> Flash AS3 FIX: Runtime Error Embedding MovieClips From External SWF –, LLC

Flash AS3 FIX: Runtime Error Embedding MovieClips From External SWF

The Problem

I’ve been trying to embedding symbols into my classes using .swf files and I’ve been getting runtime errors when I try to load some of my MovieClip symbols.

TypeError: Error #1034: Type Coercion failed: cannot convert [ClassName] to flash.display.MovieClip.

So while this works with Sprites and Buttons I can’t seem to get it working with some of my MovieClips.

import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.display.MovieClip;
[Embed(source='graphics/weather.swf', symbol='cloud')] public var cloud_sprite:Class;
[Embed(source='graphics/interface.swf', symbol='button')] public var button:Class;
[Embed(source='graphics/animations.swf', symbol='rain')] public var rain_movie:Class;

//this works
var cloud:Sprite = new cloud_sprite();

//this also works
var btn:SimpleButton = new button();

//this will give me a runtime error
var rain:MovieClip = new rain_movie();

The Solution

If you have a MovieClip that’s only one frame long you’ll need to add another frame to the symbol.  Thanks to several hours of Googling (yes this should be a word added to the dictionary) I found the solution on Bit101. For some reason AS3 casts embedded symbols based on the number of frames they have. If your symbol has more than one frame it will cast it as a MovieClip, but if it only has one frame it’ll cast it as a Sprite even if it’s defined as a MovieClip in the external swf file.

[Embed(source='graphics/weather.swf', symbol='cloud')] public var cloud_sprite:Class;

You may also like...

1 Response

  1. Pablo says:

    DAMN! Thanks! i was starting to go nuts on this one… o,0 finally!

Leave a Reply