The key is knowing that event object, that is passed to subscribed methods also carries a reference to the source of the event.
function someFunction(e:Event):void
{
// Get the reference to the button and access its name.
var aButton:DisplayObject = e.currentTarget as DisplayObject;
var aName:String = aButton.name;
// If all the buttons are named in the way you put it:
// Extract digits.
var aDigits:String = aName.substr(3);
// Convert them into number.
var anIndex:int = int(aDigits);
// Profit!
trace(anIndex);
}
UPD. As Vesper pointed out below, it needs some explanation.
There are two references in the event object: e.target and e.currentTarget, that might or might not be the same:
- e.target is the original source of event
- e.currentTarget is the object you subscribed the event handler to
Let us think how MouseEvent.CLICK occurs. You click a button. At the same time you click a MovieClip/Sprite that contains the button. All their parents. Finally, Stage. So the e:MouseEvent starts at the button so the button dispatches it, then it bubbles (this process is called bubbling, read this: Event Bubbling, and Stop Propagation) one level up, then button parent fires the event, and so on all the way up to stage.
There are also 2 properties that can change the result of event handling: