BIT-101 [2003-2017]

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

« Previous Post
Next Post »