Flash AS3 FIX: Runtime Error Embedding MovieClips From External SWF

Comment 1 Standard

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();
addChild(cloud);

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

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

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;
Advertisements

One thought on “Flash AS3 FIX: Runtime Error Embedding MovieClips From External SWF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s