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

Flash AS3: Difference between a number and int and uint

Leave a comment Standard

Number – holds decimals and numbers larger or smaller than max uint.

Uint – holds numbers randing from 0 to 4,294,967,295. Cannot contain decimals. Optimal for holding hexadecimals (ie colors) and large integers.

Int – holds numbers ranging from -2,147,483,648 to 2,147,483,647. Cannot contain decimals.