Importance of Event.clone

In Actionscript 3, the Event object has a method called clone.  This method does exactly what it says, it clones or duplicates the Event object, returning an entire new instance with the same values for all properties. The importance of this method comes into play not when you call it directly, but when the player calls it behind the scenes. As stated in the docs,

“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.”

After yesterday I feel like I don’t pay attention well, because for some reason I was thinking that clone only got called by the player when bubbling an event in the display list. Yet as clear as it is stated in the docs, this occurs in non display objects which subscribe to an event, then redispatch it using their own dispatchEvent method.  In these cases, if you don’t implement clone, then your custom event will cast to the base Event class and will not contain any of it’s properties. This will in turn throw runtime errors.

I always implement the clone method in my Event sub classes, but somehow missed it in one and was wondering why it was casting to Event. Though it confused me for a minute, at least it opened my eyes to a simple concept I overlooked. Hopefully this post opens anyone else’s who didn’t realize the player’s behavior with clone.

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">