views:

64

answers:

2

Hi guys.. I always have a question about override custom event. I am not sure why or what do to inside override function. I searched google but didn't get too many feedbacks. I appreciate if someone can light me up. Thanks.

EDIT:

My projects seem work fine even though I use my custom event without override. Anyone could explain it? Thanks.

+5  A: 

In general, you need to override a function when its use needs to be modified. So for instance, say I have a class called Car. In this class I have a function called go() which starts the car.

Now if i extend this class into another class called PickupTruck, I need to override the Car class' go function so it not only starts the car, but also attaches the Trucks trailer.

So in your case you have to override the clone method of your CustomEvent class because it should return a new CustomEvent instead of a new Event.

Fox
I was talking about overriding my own custom events in as3. For example:override public function clone():Event{ return new MyCustomEvent(this.type, this.data)}+1 though
Jerry
I like your answer but Juan's answer is closer to my question. Thanks.
Jerry
+2  A: 

From 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.

So, you can have a problem if you don't override clone and redispatch an Event. Also, the problem is not only that the custom properties will not be copied. The clone method will be called on the base class, Event. This will return an Event object, not a CustomEvent. If you have a handler that expects a CustomEvent and receives an Event, an error will be thrown and the code in your handler will not run.

Juan Pablo Califano