开发者

Can e.target use to a button

Rather than assign to every buttons with MouseEvent, I assign to AIR application with:

private function init():void {
  this.addEve开发者_如何学GontListener(MouseEvent.MOUSE_DOWN,mpressKey);
}

However, I only want the mouse_down to execute if it detect a "button" property instead of Demo0.WindowedApplicationSkin2.Group3.contentGroup.g4 (g4 is an id).


Don't rely on event.target to check if a button was clicked or not. The target property is set to the innermost item that was clicked on. When you click on a button, you're not always clicking on The Button; you might be clicking on the text field that displays the label, or the background image if any, or some other child skinning part etc - the target will be set to this inner item.

If you want to have a single click handler for all buttons and take appropriate action based on the button clicked, you can assign same function as handlers for each button and check the event.currentTarget property; when an event handler is invoked, currentTarget is set to the object with which that handler was registered.

btn1.addEventListener(MouseEvent.CLICK, clickHandler);
btn2.addEventListener(MouseEvent.CLICK, clickHandler);
btn3.addEventListener(MouseEvent.CLICK, clickHandler);

public function clickHandler(e:MouseEvent):void
{
  if(e.currentTarget == btn1){
    /* Handle btn1 here */
  }
  else if(e.currentTarget == btn2){
    /* Handle btn1 here */
  }
  else if(e.currentTarget == btn3){
    /* Handle btn1 here */
  }
}

When you add a single mouse handler using airApp.addEventListener, the currentTarget will always be your airApp and thus you can't use it to act as a single function to handle them all.


Are you asking how to test to see if the target is a Button, or a particular button?

If it's a Button

if ( e.target is Button ) { ... }

or if it's a particular button

if ( e.target == myButton ) { ... }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜