5.5.08

Anonymous Event Handlers

Oh Snap!



I've been at this (ActionScript) for a while and the longer I do it (and the more it allows me to) the more traditional I try to become. I often find myself writing a special class extended from some base class where the behavior is less than optimal and I always feel like I am really putting way too much into a simple problem to achieve a really simple goal.

This is especially true with event handlers where I need to wait for an event to make a function call with parameters. Just today I needed a generic handler for a TimerEvent. In one case the end goal was to call a function without any parameters and in the second pass an Array as an argument. Of course both cases threw an error because handler for case one expected no parameters and handler for case two expected an Array. As you know, the Timer event dispatcher blindly passes a TimerEvent to the handler. (In Flex this isnt a problem because you just bind the listener without the 'event' argument.)

Each case has a workaround, case one requires a new function (to scrub the event parameter) the second case requires a class to store a single instance of the array as a parameter. In an environment where developers loath new classes and lots of variable definitions I was searching for another way to handle these situations. I found this simple solution from Phantasmagoria
http://www.kongregate.com/forums/4/topics/6272?page=1

Basically its a function that takes the same parameters as the function you need to call and returns an anonymous function that accepts the Event type thrown by Timer. The returned function calls the function that you really want to call with a copy of the parameter(s) you specify (stored in a temporary scope.) At the end of the day your argument values are stored and your function is called without a new class or function call.

Here's a sample AS3 application depicting the second case.

08.08.04 One thing I wanted to add to this post. Say you have an array of objects and you want to set up separate handlers for each object in the array. Don't get smart and use an anonymous function. Because the handler will be overwritten in the next iteration (even if you use the "var" keyword) and all your handlers will actually be references to the last handler created. Instead pass the variables you want to roll into a class function that returns the handler (like in the sample above.) This guarantees that function scope variables are in a new function scope, not in the scope of the iterator.