After adding a custom event to something I was coding up today I started intermittently receiving something like this error message:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@148cb29 to com.brokenfunction.CustomEvent

It came with no stack trace, no line numbers, and had no determinable source. In other words, it was incredibly annoying. I had recieved this message before because I had not implemented clone(), but clone() was working properly now, what could be the cause? I pressed the play/pause button on my keyboard while pondering this and it triggered the error message. More research indicated it had nothing to do with any keyboard events, and was actually due to the player apparently quickly losing and then gaining focus. Why my OS sees fit to do this is still an unsolved mystery.

For my CustomEvent I had a couple types, including ACTIVATE and DEACTIVATE, which are already handled by Event but I added them anyway (var ACTIVATE:String = Event.ACTIVATE) so I wouldn’t even have to import Event when using my CustomEvent. So, on a hunch I changed the values of ACTIVATE to a different string and it fixed the bug!

The EventDispatcher I used to dispatch my CustomEvents was completely isolated so I foolishly thought it wouldn’t actually start broadcasting events implicitly – I was wrong. EventDispatcher still dispatches ACTIVATE/DEACTIVATE (when the player focuses/blurs) no matter what. I presume the error message came up when listeners were expecting a CustomEvent and got an Event instead. The lack of a stack trace could be explained by the fact that there is no stack to trace, as the caller is some Flash internal somewhere and it never actually gets to my code because of the errors.

2 Responses to “Event type coercion adventures”

  1. Dan Says:

    Max,

    Thanks bunches for this post. I just spent an inordinate amount of time looking for the same thing. Everything I could was pointing to the clone() method as well and completely overlooked that the ACTIVATE type was already being used. Thanks for sharing.

  2. FreeSoft Says:

    просто афигенно!!!!))

Leave a Reply