February 08, 2008

Flex/Cairngorm custom event problem

I came across a problem yesterday where I was using CairngormEventDispatcher to dispatch an event each time a button was clicked. I defined a Cairngorm event instance then triggered it on the click event:

import uk.ac.warwick.recorder.control.StopEvent;
private var myEvent:StopEvent = new StopEvent();
<mx:Button id="stopBtn" label="Stop" 
height="25" width="55" click="CairngormEventDispatcher.getInstance().dispatchEvent(myEvent);"/>

Which seemed to work fine, until I pressed the button again, at which point the debugger threw this exception:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@91ce331 to com.adobe.cairngorm.control.CairngormEvent.

The reason for this is that Flash creates an internal clone for an event that has already been dispatched, and importantly the redispatched event is no longer a Cairngorm Event, it’s now just a Plain Old Event. This is true for any custom event in Flex/Flash. One workaround is to generate a new instance of the event type each time it needs to be dispatched, like this:

import uk.ac.warwick.recorder.control.StopEvent;
<mx:Button id="stopBtn" label="Stop" height="25" width="55" click="CairngormEventDispatcher.getInstance().dispatchEvent(new StopEvent());"/>

There are other ways to do this, including overriding the event clone method, but the above solution worked for me.

- No comments Not publicly viewable

Add a comment

You are not allowed to comment on this entry as it has restricted commenting permissions.


MXNA link



    Other blogs I like...

    Black Pepper Software

    Eismann-sf Go to 'Comments on: Design News for Web, Graphic Designers'

    Ted On Flex Go to 'Ted On Flash'



    Not signed in
    Sign in

    Powered by BlogBuilder