Through the use of event types
you can easily reduce code bloat that often occurs when defining events for many objects on stage by filtering events in 1 function rather than defining many event handling functions.
Imagine we have 10 objects on stage named object1
, object2
... object10
You could do the following:
var i: int = 1;
while(getChildByName("object"+i) != null){
var obj = getChildByName("object"+i)
obj.addEventListener(MouseEvent.CLICK, ObjectMouseEventHandler);
obj.addEventListener(MouseEvent.MOUSE_OVER, ObjectMouseEventHandler);
obj.addEventListener(MouseEvent.MOUSE_OUT, ObjectMouseEventHandler);
obj.alpha = 0.75;
i++;
}
function ObjectMouseEventHandler(evt:Event)
{
if(evt.type == "click")
{
trace(evt.currentTarget + " has been clicked");
}
else
{
evt.currentTarget.alpha = evt.type == "mouseOver" ? 1 : 0.75;
}
}
Benefits to this method include: