Dispatching custom event in handler for other event [AS3]

By | August 6, 2009

I’ve been struggling with an issue that I can’t seem to understand, but I found a workaround so I have to move on.  Maybe someone out there will have a better grasp of what is going on.

In my event handler for a Timer object, I dispatch a custom event, but not the event that came into the handler.  To try to save some space, since my custom event could be called a lot of times, I keep a single copy of the event in my custom class.  Here is my Timer object event handler:

   private function timerElapsed (ev:Event) : void {

In the initialization of my class, I create the pulseEvent object:

   internal var pulseEvent:EventWithData;
   pulseEvent =  new EventWithData(PulseActivity.TICK, false, false, customData);

When I run the code, the pulseEvent is sent along fine the FIRST time.  The second and subsequent times, Flash sends a different event which does not have the custom data.

This led me to suspect something to do with clone(), judging from the AS3 documentation:

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.

The odd part in this is that I’m not redispatching the event coming in — I’m dispatching a different event (the one I’ve created and stored in the custom class).  I suspect somehow my new event dispatch is getting caught up in this and Flash is cloning the event, but since I didn’t create an override (lazy me) for clone, I’m paying the price.

Rather than going this route, I realized I don’t really need my custom Event class, rather, I can use Event and use the target property (ev.target) to get to the object dispatching the event, then make a property off that class to store the custom info I want (Whew!).  I am a bit unsettled with all the potential cloning going on, but I am under a deadline so I will have to revisit it in the future (which could be way in the future).  I hate these kinds of loose ends!

[8/6/09 Update, 14:20.  Yes, it turned out to be the clone issue.  When I correctly added a clone override, the original code worked.  I think this means that the space I’m trying to save is not working because clone gets invoked to create a new event.  Oh, well, can’t say I didn’t try.  If I see a problem regarding the load, maybe I will try to address it again in the future]

Leave a Reply

Your email address will not be published. Required fields are marked *