Redispatching Custom Events in AS3

Another one of those “I’ll blog this so 6 months from now, when I run into this again, I’ll remember that I blogged something about this and search my own blog for the answer” posts. 🙂

I ran into this last week, redispatching a custom event that caused all kinds of problems. After I found the solution, I remember someone told me about this months ago.

clone () method
public function clone():Event
Duplicates an instance of an Event subclass.

Returns a new Event object that is a copy of the original instance of the Event object. You do not normally call clone(); the EventDispatcher class calls it automatically when you redispatch an event—that is, when you call dispatchEvent(event) from a handler that is handling event.

The new Event object includes all the properties of the original.

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. If you do not set all the properties that you add in your event subclass, those properties will not have the correct values when listeners handle the redispatched event.

In this example, PingEvent is a subclass of Event and therefore implements its own version of clone().

[as]class PingEvent extends Event {
var URL:String;

public override function clone():Event {
return new PingEvent(type, bubbles, cancelable, URL);
}
}[/as]

Returns
Event — A new Event object that is identical to the original.
See also

Event objects

This entry was posted in Flash. Bookmark the permalink.

8 Responses to Redispatching Custom Events in AS3

  1. Eric Cancil says:

    Might be a good Idea to make an event that extends clone that dynamically loops through properties and sets them…that you can extend from

  2. kp says:

    it’s not just the properties, you have to have clone return the correct type. i suppose you could do that dynamically by finding the class and making an instance of it, but the clone method will usually be only a single line anyway: return new MyCustomEvent(params…); Easy enough to just get in the habit of adding that.

  3. liamwalsh says:

    eclipse templates to the rescue…

  4. Just saved me from a night head-ache. I remember reading something about this stuff long time ago, and now when I had trouble with it myself the problem was finding the solution.

    Thanks Keith and Google Reader.:)

  5. Simon says:

    Thank you! You saved me at least an hour of debugging.

  6. shaman4d says:

    Thanks Keith! I loving your site more and more.

  7. Fábio Magagnin says:

    Nice Post! I was trying to redispatch and event and receiving an error telling that flex couldn’t convert from my ‘CustomEvent’ to ‘Event’. Overriding de Event.clone() solved the problem. Thanks.

    But, to me it sound’s a little weird when I have to redispatch an event, I was wondering if flex has a better aproach to it, or if there is something wrong with my code. Any comments?

  8. dimumurray says:

    Here’s something I have always wondered about the clone method. Why is it required internally? Why not simply re-dispatch an event without cloning it ?

Leave a Reply