{"id":45,"date":"2009-08-06T13:36:51","date_gmt":"2009-08-06T13:36:51","guid":{"rendered":"http:\/\/www.eqsim.com\/blog\/?p=45"},"modified":"2010-02-23T13:37:19","modified_gmt":"2010-02-23T13:37:19","slug":"dispatching-custom-event-in-handler-for-other-event-as3","status":"publish","type":"post","link":"https:\/\/www.eqsim.com\/blog\/dispatching-custom-event-in-handler-for-other-event-as3\/","title":{"rendered":"Dispatching custom event in handler for other event [AS3]"},"content":{"rendered":"<p>I\u2019ve been struggling with an issue that I can\u2019t seem to understand, but I found a workaround so I have to move on.\u00a0 Maybe someone out there will have a better grasp of what is going on.<\/p>\n<p>In my\u00a0event handler for a Timer object, I dispatch a custom event, but not the event that came into the handler.\u00a0 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.\u00a0 Here is my Timer object event handler:<\/p>\n<pre>   private function timerElapsed (ev:Event) : void {\r\n      dispatchEvent(pulseEvent);\r\n   }<\/pre>\n<p>In the initialization of my class, I create the pulseEvent object:<\/p>\n<pre>   internal var pulseEvent:EventWithData;\r\n   ...\r\n   pulseEvent =\u00a0 new EventWithData(PulseActivity.TICK, false, false, customData);<\/pre>\n<p>When I run the code, the pulseEvent is sent along fine the FIRST time.\u00a0 The second and subsequent times, Flash sends a different event which does not have the custom data.<\/p>\n<p>This led me to suspect something to do with <code>clone()<\/code>, judging from the AS3 documentation:<\/p>\n<p>Returns a new Event object that is a copy of the original instance of the Event object. You do not normally call <code>clone()<\/code>; the EventDispatcher class calls it automatically when you redispatch an event\u2014that is, when you call <code>dispatchEvent(event)<\/code> from a handler that is handling <code>event<\/code>.<\/p>\n<p>The new Event object includes all the properties of the original.<\/p>\n<p>When creating your own custom Event class, you must override the inherited <code>Event.clone()<\/code> 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.<\/p>\n<p>The odd part in this is that I\u2019m not redispatching the event coming in \u2014 I\u2019m dispatching a different event (the one I\u2019ve created and stored in the custom class).\u00a0 I suspect somehow my new event dispatch is getting caught up in this and Flash is cloning the event, but since I didn\u2019t create an override (lazy me) for clone, I\u2019m paying the price.<\/p>\n<p>Rather than going this route, I realized I don\u2019t really need my custom Event class, rather, I can use <code>Event<\/code> and use the target property (<code>ev.target<\/code>) to get to the object dispatching the event, then make a property off that class to store the custom info I want (Whew!).\u00a0 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).\u00a0 I hate these kinds of loose ends!<\/p>\n<p><strong>[8\/6\/09 Update, 14:20.\u00a0 Yes, it turned out to be the clone issue.\u00a0 When\u00a0I correctly added a clone override, the original code worked.\u00a0 I think this means that the space I&#8217;m trying to save is not working because clone gets invoked to create a new event.\u00a0 Oh, well, can&#8217;t say I didn&#8217;t try.\u00a0 If I see a problem regarding the load, maybe I will try to address it again in the future]<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve been struggling with an issue that I can\u2019t seem to understand, but I found a workaround so I have to move on.\u00a0 Maybe someone out there will have a better grasp of what is going on. In my\u00a0event handler for a Timer object, I dispatch a custom event, but not the event that came\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.eqsim.com\/blog\/dispatching-custom-event-in-handler-for-other-event-as3\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19,17],"tags":[],"_links":{"self":[{"href":"https:\/\/www.eqsim.com\/blog\/wp-json\/wp\/v2\/posts\/45"}],"collection":[{"href":"https:\/\/www.eqsim.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.eqsim.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.eqsim.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.eqsim.com\/blog\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":1,"href":"https:\/\/www.eqsim.com\/blog\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"predecessor-version":[{"id":46,"href":"https:\/\/www.eqsim.com\/blog\/wp-json\/wp\/v2\/posts\/45\/revisions\/46"}],"wp:attachment":[{"href":"https:\/\/www.eqsim.com\/blog\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.eqsim.com\/blog\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.eqsim.com\/blog\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}