Wednesday, April 8, 2009

Figuring out which button is pushed

A simple one. Suppose we create two buttons: one called offButton with a label value of "off" and another one onButton with a label value of "on".

Then, if we add event listeners to both:
onButton.addEventListener("click", buttonPushed);
offButton.addEventListener("click", buttonPushed);


we can tell which one we pushed with this function:

function buttonPushed(evt:Object):Void{
var label:String = evt.target.label;
if(label == "off")
{
trace("You pushed the off button");
}else if(label == "off")
{
trace("You pushed the on button");
}
}

0 comments: