Optimization Question: 1 handler for multiple events or 1 handler per event?
Something I'm having trouble deciding, simply because I'm not sure which would be more effective.
Example: I have a couple menus, each with a handful of icons to click on (lets say a total of 10 objects that can be clicked on).
Would it be better to have 1 stage.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler); with 10 if(e.target.name == "nameX") that gets called on every click.
Or would it be better to have 10 separate objectX.addEventListener(MouseEvent.MOUSE_DOWN, clickHandlerX); for each menu item that can be clicked on?
I guess my confusion comes from not knowing what a listener is doing exactly, is it using any resources watching for an event?
(added)
After typing all that out, would a correct comparison be closer to an event listener is nothing but a way of calling a function and otherwise isn't using resources?
My question still remains, but I'm leaning towards multiple listeners and handlers.
(added 2)
Sorry my brain scrambles, I'm not good at explaining things. I think this is a bit clearer.
For 1 menu I have 1 this.addEventListener, and in the handler it has multiple if (menu_itemX.name == "nameX").
Is it better to have 1 eventListener in this situation or would it be better to have multiple menu_itemX.addEventListener and seperate handlers for each action?
(followup question)
I've been doing addEventListener when the menu opens and removeEventListener when the menu closes. If event listeners do use "no" resources unless their event is triggered is this a bad practice for simple click and mouseover and mouseout events? Should I just leave the event listeners there all the time?
(code example)
If this makes it any clearer, which of these would work best?
Version 1:
this.menu1.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler1);
this.menu2.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler2);
this.menu3.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler3);
this.menu4.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler4);
this.menu5.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler5);
function clickHandler1(e:Event):void{}
function clickHandler2(e:Event):void{}
function clickHandler3(e:Event):void{}
function clickHandler4(e:Event):void{}
function clickHandler5(e:Event):void{}
version 2:
this.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler);
function clickhandler(e:Event):void{
if(e.target.name == "menu1"){}
else if(e.target.name == "menu2"){}
else if(e.target.name == "menu3"){}
else if(e.target.name == "menu4"){}
else if(e.target.name == "menu5"){}
}