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.

4.4.08

Assign a class to an instance

Phatness



So its been a while since I've posted anything. My new job has been keeping me really busy. I thought I would write about Object.registerClass and talk about how I am using __proto__ to allow a class to become a specific target.

For seasoned veterans this is probably a part of your AS2 library of tricks. Until recently I had become accustomed to using Object.registerClass to accomplish this. The process was pretty straight forward.

1. Register the library item
2. Attach the clip
3. Un-register the library item (so that we don't later inadvertently re-attach an instance of the class in a later call)




This works great for instantiating a new instance of a class without having to link a library item explicitly at design-time and it allows you to attach classes anywhere at will during run-time. But you have to make sure that every swf has "_blank" in its library and you can never instantiate as instance of a class at the root. There are other drawbacks which I wont go into here.

Introducing the super handy "register" function. You can name your function anything you like: ("infect","control","become","instantiateAs" etc...) you dont need any library items, no design time links, and so on and so on.



Of course you want to replace MyClass with your actual class name and its probably a good idea to create a "main" function or something to run your initialization code from. The register function is static so you wont have an instance of this, just call target.main() and create a non-static function in your class. Then to register a target as a specific class you just call its register function and pass it a target.



This one line of code will even instantiate _level0, (Yes, _root) as your class, now for AS2, that is about as good as it gets.