I'm implementing a text display area inside an app that displays selected text when the user mouses over one of four elements. Rather than creating a handler function for each element, I would like to get the name of the instance that is calling the handler in order to implement a switch statement. I've tried two ways, but both aren't working:
//install event handlers
initialText.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
timeText.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
withdrawalText.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
//also tried without toString, same result
var name:String= evt.target.name.toString();
var name=String= evt.currentTarget.name.toString();
Both of those return undefined for the variable name. However, in the debugger, I can trace the event values through currentTarget.name, a开发者_Python百科nd that shows the instance firing the handler function, whether it be withdrawalText, initialText or timeText
. So how can I apply the name value to a variable in order to determine which text block to display?
for each (var field:TextField in [initialText, timeText, withdrawlText])
field.addEventListener(MouseEvent.MOUSE_OVER, mouseOverEventHandler);
function mouseOverEventHandler(evt:MouseEvent):void
{
switch (evt.currentTarget)
{
case initialText: /*initialText specific code*/ break;
case timeText: /*timeText specific code*/ break;
case withdrawalText: /*withdrawalText specific code*/
}
}
this is untested, but it should work as long as the scope of your text field instance variables reach the mouseOverHandler.
精彩评论