April 3, 2008

Implement clone() in custom events

Filed under: dev, education, flex, tip — michaelangela @ 1:16 am

I have custom components that “relay” events. They pick up events from one place and pass them to the other. (There is a place for that… sometimes… somewhere…) I have not hit this error myself, but the lesson is still a valid one. How do you pass an event and make sure it is passed correctly with all data intact? As I think back, I may have gotten the events to pass before but for reasons previously unknown, the data disappeared. I didn’t know why then, and I think this explains it.

Daniel R.: Events in Flex 2

Today I got a familiar error while working with some event code:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@35f04c1 to com.example.events.CustomEvent.

The short answer is that this error results from failing to
implement a clone() method on a custom event class. The long answer is
that most of the time not implementing a clone() method won’t hurt you,
based on how you use your custom events, but you really should create one. In fact the Actionscript 3 (AS3) documentation is pretty clear about this:

When creating your own custom Event class, you must
override the inherited Event.clone() method in order for it to
duplicate the properties of your custom class.

This is a time that I wish AS3 had abstract methods.

I say you don’t really need a clone() method is that it only becomes an
issue if you relay an event. The follow code is an example of event

private function relay(customEvent:CustomEvent):void


In this case an event listener redispatches the event that it got. When
this happens the event framework behind the scenes calls the clone()
method to create a new instance of the event. If you don’t have a
clone() method and the next listener in the chain is expecting an
instance of CustomEvent, the error from above happens. If instead that
same listener was only expecting an instance of the Event class, it
would work, but any additional information contained in CustomEvent
class would be lost, including the ability to cast it to an instance of



  1. […] you don’t override clone, your custom Event won’t be redispatched correctly. This could lead to a type coercion error if you’re lucky. In worse cases, it might fail silently, leaving you pulling your hair for […]

    Pingback by Manish Jethani » Blog Archive » Why it's important to override clone in your custom Event class — April 4, 2008 @ 4:17 pm

  2. While you might not have abstract methods, you do have the Proxy class. 🙂

    Comment by Allen Ellison — April 30, 2009 @ 12:38 am

    • I haven’t used the Proxy class before. But using this I do see how it’s possible to make stub functions that are then overridden using the Proxy class as a sort of abstract method workaround. Thanks for the tip!

      Comment by michaelangela — November 24, 2009 @ 2:56 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: