Flash to Flex ComponentEvent coercion error solved

Today I had an interesting problem embedding a swf file compiled in Flash Professional CS3 into Flash Builder 4. My SWF file contained a Flash TextArea component, which dispatches a ComponentEvent when scroll bars are activated. This event resulted in the following error in Flex:

TypeError: Error #1034: Type Coercion failed: cannot convert
fl.events::[email protected] to mx.events.FlexEvent. at
flash.events::EventDispatcher/dispatchEventFunction() at
flash.events::EventDispatcher/dispatchEvent() at fl.core::UIComponent/set visible() at
fl.controls::ScrollBar/updateThumb() at fl.controls::ScrollBar/set enabled() at
fl.controls::ScrollBar/setScrollProperties() at fl.controls::UIScrollBar/setScrollProperties()
at fl.controls::UIScrollBar/updateScrollTargetProperties() at
fl.controls::UIScrollBar/handleTargetChange()

The first thing I needed to figure out was what type of ComponentEvent my TextArea was firing when scroll bar was activated. After a bit of debugging it turned out it was ComponentEvent.SHOW (weird).

Then I wrote a little event handler in Flex that would catch this event and cast it to an object because Flex doesn’t know about ComponentEvent.

swfLoader.addEventListener(FlexEvent.SHOW, onScroll);
private function onScroll(event:*):void {
event.preventDefault();
event.stopImmediatePropagation();
}

That fixed it. Remember, you need to cast the event to ‘any (*)’ ¬†and call stopImmediatePropagation() method to stop Flex from processing any further event listeners.

Cheers
Marko