assertTrue is the professional blog of Luke Bayes and Ali Mills

Custom Events in ActionScript 3.0

Posted by Luke Bayes Sun, 15 Oct 2006 06:18:00 GMT

I just lost at least 2 hours to this one and figured I’d share with the hope that maybe I’ll save someone else the same headache…

If you’re working with the Flex 2 framework and writing custom components as directed, you’re probably creating custom event data types.

If your event handlers are written to expect an instance of your custom data type, be sure you override the “clone” method and return a new, configured instance of your new event data type.

I’ve been staring at this error message for hours now:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@148cb29 to lifebin.events.ActionEvent.

It was driving me nuts because the stack trace begins with a method that actually dispatches a correctly configured instance of an ActionEvent!

I even went so far as to create isolated test cases that create, dispatch and bubble custom events through a Flex and non-flex composite structure. I was unable to reproduce the error outside of our application and still don’t know why or whom is calling clone – but I just tried implementing the clone method out of desparation and voila! Everything works.

I’d love to know why this is happening – but for now – I’m content with spreading the word as to how to avoid it!

Good Luck.

Tags  | 3 comments

Comments

  1. senocular commented: Avatar The Flash Player internally clones your events when they are redispatched (either explicitly using dispatchEvent to dispatch an existing event or when an event bubbles) creating a new instance that is required for the new target variables. The docs indicate that this is required ( though, technically, its only required when redispatching): http://livedocs.macromedia.com/flex/2/langref/flash/events/Event.html#clone() Of course being located in the clone method description, one might never know that to be the case (honestly, who reads the clone documentation?).
    Posted: about 9 hours later.
  2. Stephen Gilson replied: Avatar There is a chapter in the "Creating and Extending Flex 2 Components" doc on creating custom event classes:

    http://livedocs.macromedia.com/flex/2/docs/00001640.html

    Including an example defining clone():

    http://livedocs.macromedia.com/flex/2/docs/00001644.html

    Stephen
    Posted: 1 day later.
  3. Luke Bayes commented: Avatar

    Thanks for the comments guys, I figured there would be something written somewhere.

    I did create a separate test case that created two simple composite structures, one using Flex controls and one that was not. I then passed a custom event data type through these composite structures with bubbling turned on and didn't get any errors. My custom event in that case did not override clone, and my handlers appeared to receive objects of the appropriate data type.

    I would love to know more about when and why clone is called.

    Overall, this seems like a strange decision to me. If I stuff an instance of an object into one end of the event dispatching system, I expect the same instance to pop out into my handlers, not some cloned version.

    Posted: 1 day later.

Your Reply

Comment Form.

Fields denoted with an "*" are required.