views:

18

answers:

2

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

private function init():void {
  this.addEventListener(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).

+1  A: 

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 ) { ... }
Gregor Kiddie
Oh, yes, I used this "is" to identify component on other project months before but cleanly forgot. Thanks!
JohnMax
Accept the answer then! You need to start accepting.
Gregor Kiddie
+2  A: 

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.

Amarghosh
Won't adding 200's eventlistener or more caused performance lag? I have to ensure the latency MIDI keyboard to flash application is within an acceptable timing.
JohnMax