Suppose I want to create a single button. This should be easy enough - just create a square, addChild to add it to the screen and an event listener for mouse.CLICK event
add_btn_listeners():void
{
btn[0].addEventListener(MouseEvent.CLICK, btn_clc1);
}
public function btn_clc1(event:Event):void
{
action1();
}
Suppose though that you wanted to create twenty buttons. You would then need twenty functions similar to the above btn_clc1 function with an event listener for the appropriate click.
But suppose you wanted the action to very slightly such as by index. For example, btn[0] calling action1, btn[1] calling action2, etc. in the same listener btn_clc1 listener.
A very common example of this would be mouse rollover. On rollover to highlight a square for instance, increasing the alpha layer to highlight a menu selection. The highlighted layer would depend on the index, something like: btn[index].alpha = .9;
Is there a way to reduce the number of event listeners, or code more optimally in cases like this? Most of the example's I've seen seem kind of shallow for larger cases.