2012-07-17 65 views
1

我有一个弹出窗口,我想关闭它在鼠标外点击。所以我做了事件的收听者MouseDownOutside。但它只听左键单击。我也想关闭它右键点击,但如果我右击弹出,然后它应该打开。我在谷歌搜索,但第二次它没有任何解决方案。有没有人知道关闭弹出窗口的方法?想要在弹出窗口之外听右键单击。

这样一个弹出的创建代码如下所示

var myPopUp:CustomComponentClass = new CustomComponentClass(); 
PopUpManager.addPopUp(myPopUp, Parent, true); 
PopUpManager.centerPopUp(myPopUp); 

关闭弹出窗口

PopUpManager.removePopUp(myPopUp); 

在此先感谢

回答

1

我会尝试这样的事:

stage.addEventListener(MouseEvent.RIGHT_MOUSE_DOWN,onRightMouseDown); 

function onRightMouseDown(e:MouseEvent):void 
{ 
    var point:Point = popup.globalToLocal(new Point(stage.mouseX,stage.mouseY)); 

    if(!pointInside(popup,point)) 
    { 
    PopUpManager.removePopUp(popup); 
    } 
} 

pointInside函数只是检查点是否在弹出框的矩形内。

public function pointInside(obj:DisplayObject, point:Point):Boolean 
{ 
    return (point.x >= 0 && point.y >= 0 && point.x <= obj.width && point.y <= obj.height); 
} 
+0

你会这么做......怎么样? – 2012-07-17 14:28:00

+1

添加了一个函数,因为该点已转换为显示对象的局部坐标,只需检查0,0和宽度高度就可以做到,或者getBounds(stage).contains(x,y)可用于检查点是否也在里面。 – 2012-07-17 14:42:58