A common issue I am having with as3 at the moment, I wanted to get some ideas on to see if anyone has a better solution.
I want to use a method for an event and also call it directly without having to double up on methods, not a biggy but really anoying when you have lots of buttons. If I try to call the method directly flash complains that I have the wrong amount of args and if I leave out the event flash complains.
maybe some code would help, here is what I am doing
private function setupButton():void {
_btn.addEventListener( MouseEvent.MOUSE_UP, pressedE );
// call directly first time for instance pressed();
}
private function pressedE( e: Event ) {
pressed();
}
private function pressed():void {
trace('all that for a trace!');
}
so how do I do this without having both pressedE and pressed, ideas welcome.
private function setupButton():void { _btn.addEventListener( MouseEvent.MOUSE_UP, pressedE );
// call directly first time for instance
pressedE();
}
private function pressedE( e: Event = null ) { trace('all that for a trace!'); }
thanks Arsis that is really usefull tip/trick.
yeap
or you can pass null as a parameter to the method
private function setupButton():void { _btn.addEventListener( MouseEvent.MOUSE_UP, pressedE );
// call directly first time for instance
pressedE( null );
}
private function pressedE( e: Event ) { trace('all that for a trace!'); }
alternatively, if pressedE needs the event...
private function setupButton():void { _btn.addEventListener( MouseEvent.MOUSE_UP, pressedE );
// call directly first time for instance
_btn.dispatchEvent(new MouseEvent(MouseEvent.Mouse_UP));
}
private function pressedE( e: Event ) { trace('all that for a trace!'); }
good coverage guys these are real gems, much more than I expected but everything I need.
ah good one baron!
i'm a code snob. i would never call an event handler directly. bad practice. i would write it exactly has you did in the first post. for a quick, one-off app you are the only developer on, ok, but if another developer ever needs to work with that code, it's going to confuse them.
It would seem relevant for flashes typical language domain that AS4 should allow listeners to call methods that may or may not require a parameter, maybe bit you or others with a voice could open a debate given the differences between languages such as java and ruby that may allow different solutions as the
private function pressedE( e: Event = null ) { trace('all that for a trace!'); }
seems less ideal than
private function pressedE( ) { trace('all that for a trace!'); }
If an event info is not used then why should it be needed?
"If an event info is not used then why should it be needed?"
How does the event dispatcher know whether or not you need it?
The question isn't so much the listener as the dispatcher. An event dispatcher dispatches an event. It sends along an event object that has important information about the event. Although it's not ALWAYS needed, it's pretty damn important in a LOT of cases, so you can't just say "don't send the event object," as that would surely cause a zillion more problems than is solves.
As for the listener, because it is simply a function that is called by the event dispatcher, and because the event dispatcher calls it with an argument of type Event, the listener has to take an event as a parameter.
What you are asking for is to allow code to pass arguments to methods that don't accept arguments. That would not be a step in the right direction. Furthermore, the solution you want is as simple as the first example you just gave - use a default null parameter. But again, I go back to the idea that using a function as an event handler AND calling it directly is bad form.
You are trying to solve a problem of having too many methods. I bet there is some pattern that would help you more than petitioning Adobe to change the way ActionScript works. 
I'm with Bit here too, and a bit narked at myself for having read this thread before and not realised that I'd also go with 2 methods.
And I think you might benefit from renaming your methods too - it's perhaps a small thing but it might help you to clarify what your intentions for each method are.
e.g. method 'pressedE' is the event handler for the button mouse up event, that might be ok as it is although I'd probably be more verbose about it. But method 'pressed' really has nothing to do with the button mouse up event, because you also need to call it manually. So think about what that method actually does, and give it a name that reflects that action.
And maybe you could abstract the whole thing into a new class, since, as you say, you need to do this for a lot of buttons?
because the event dispatcher calls it with an argument of type Event, the listener has to take an event as a parameter.
From a purely theoretical point of view, can we create custom events such that the listener does not take an event as a parameter?
Originally posted by: JLM
because the event dispatcher calls it with an argument of type Event, the listener has to take an event as a parameter.
From a purely theoretical point of view, can we create custom events such that the listener does not take an event as a parameter?
Yes, if you dispatch them from a custom event dispatcher. Again, the problem (though I don't see it as a problem) is that "dispatching an event" really means "calling a method". The method (your listener) is called from EventDispatcher. And when it calls it, it passes the event as an argument.