Flexible Event Handler function

Is there a way to create a function that handle events (especially MouseEvents) and still call this function as a plain one without passing any arguments? All this time, I just create an extra function that handles the mouseEvent and call the needed function inside it.

Example:

function handler(e:MouseEvent):void{
realFunction();
}

function realFunction():void{
// I want this function to work as a handler and at the same time a plain function
}

Just thought that maybe there’s a better way of doing it.

Thanks in advance. :slight_smile:

The way that I do this (which is also the way that I’ve seen suggested by others) is to give the event parameter a default value, making it optional:

function realFunction(e:MouseEvent = null):void{
    // I want this function to work as a handler and at the same time a plain function
}

Coool. Just tried it and it works fine. Thanks for the quick response. :slight_smile:

Oh… wait, a follow-up to a related topic. What about event handlers that can also take an extra custom parameter? Is that possible? If it is then how do you add the event listener?

In cases like this I create a global variable that is used as reference to the event handler. Can you extend an event and add the extra parameter to it. Just thought again that maybe there’s a better way.

Thanks in advance. :slight_smile:

What about event handlers that can also take an extra custom parameter? Is that possible?

Sure, you could have such a function:

public function handler(e:Event = null, extra:Object = null):void {}

If you declared the function like that, then you would have to call handler(null, someExtraValue) when calling the function without going through EventDispatcher. When using it as a handler, then you would need to call addEventListener like so:

addEventListener(SomeEvent.SOME_CONSTANT, function(e:Event):void { handle(e, someExtraValue) });

That process could be kind of annoying, so you could also declare the handler as:

public function handler(...args):void {}

… and handle all of the arguments by detecting their type inside of the function.

Instead of adding an extra parameter to the function, I think that most people use custom Event subclasses that have a property that holds the extra value. That way, the value can be accessed with event.myExtraValue. Of course, that method doesn’t really lend itself to functions that are supposed to be able to be used without an accompanying event, but you could always add an optional second parameter and check to see whether or not a valid Event reference was passed in and then decide whether or not you want to check the second argument.

If you are wondering whether or not you can convince EventDispatcher to pass additional arguments along with the Event into your handler, then I think that the answer is ‘no’, since dispatchEvent calls the handler at some point, and there’s no way to modify the call that it makes and and override would need to fit the method signature anyway.

I see… thanks for the knowledge share! :slight_smile: