Allmas

  • Increase font size
  • Default font size
  • Decrease font size

Always override the clone() method for your custom events

E-mail Print PDF

When implementing a custom event in a Flash or Flex application, most examples and articles I found on the web don't override the clone() method. This works probably in most simple cases, but as soon as you try to redispatch the event (by calling dispatchEvent(event) in a handler that is handling event), you can get a "Type Coercion Failed" error. It states that it cannot convert flash.events.Event to your custom event's type.

Why is this happening? When you redispatch an event , the EventDispatcher class calls the clone() method and dispatches the cloned copy of the event. If you don't override the method to return an instance of the custom event, the original Event.clone() method will be called and an object of type Event will be returned. Now if you have an event listener expecting an instance of the custom event, the error will occur as the Flash virtual machine doesn't know how to convert from Event to your custom event type.

 
Comments (4)
Clone
4Monday, 21 February 2011 09:49
Sam
Nice list!! thanks to share this..by Mr. groupon clone
Nice Tip~ Thank you~:)
3Tuesday, 24 March 2009 08:58
Ki-Han Shin
It is good tip. Very Thank you~:)
Good tips!
2Wednesday, 04 February 2009 04:33
Hyder Alamgir
Always wondered why that happened :D

Good tips, both by you and Florian
Cloning
1Saturday, 25 October 2008 10:58
Florian Salihovic
I also encourage people to implement explicitly. Ths helps people to understand what they are doing while they are doing it. Allthough the IEventDispatcher might call clone() under the hood, the read-only property target must be reset, if the dispatcher is not the original target.

Since this can not be done - a new Event must be instanciated. In order to save time and redundant code, the smart programmer will override Event.clone():Event ;)

Add your comment

BoldItalicUnderlineStrikethroughSubscriptSuperscriptEmailHyperlinkOrdered listUnordered listQuoteCode
Your name:
Your email:
Subject:
Comment:
View Haykel Ben Jemia's profile on LinkedIn

Shared Items