如何获取事件侦听器的目标变量引用名称
var key1:BtnKey=new BtnKey;
var key2:BtnKey=new BtnKey;
key1.addEventListener(MouseEvent.CLICK,function(e:MouseEvent):void{
checkAnswer(e,qset)
});
key2.addEventListener(MouseEvent.CLICK,function(e:MouseEvent):void{
checkAnswer(e,qset)
});
function checkAnswer(e:MouseEvent,qset:Number):void{
//here I want the target variable reference ("key1" or "key2")
//e.target only gives the movieclip refernce like "[Object BtnKey]"
}
`
答案 0 :(得分:1)
使用e.currentTarget
获取点击的按钮。如果您需要了解自己是key1
还是key2
,请使用严格的相等比较:
if (e.currentTarget === key1) {
// Do something
} else if (e.currentTarget === key2) {
// Do something else
}