views:

17

answers:

1

Hello,

I've been working on creating a button using a MovieClip. I'm using the following events:

 this.addEventListener(MouseEvent.CLICK,OnClick);
 this.addEventListener(MouseEvent.ROLL_OVER,OnButtonRollOver);
 this.addEventListener(MouseEvent.ROLL_OUT,OnButtonRollOut);
 this.addEventListener(MouseEvent.MOUSE_DOWN,OnMouseDown);
 this.addEventListener(MouseEvent.MOUSE_UP,OnMouseUp);

Everything works fine with the exception of when I click the button and [without releasing the mouse button] I drag the mouse out of the button's focus and then I release the mouse button the OnMouseUp event is not called.

How can I fix this? Thanks, Y_Y.

+2  A: 

Inside your OnMouseDown handler you can add the following to ensure you get the MouseEvent.MOUSE_UP event you desire:

private function OnMouseDown(e:MouseEvent):void
{
    stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
}

private function onStageMouseUp(e:MouseEvent):void
{
    stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
    // handle mouse up here
}

Of course this means you'll have to do some extra work to make sure handlers are being added/removed appropriately. You might also want to cache a reference to the target button in your OnMouseDown handler in the event the scenario you describe occurs and you still need to know which button was pressed (assuming your handlers are outside the scope of the button itself).

heavilyinvolved