我有一个菜单弹出,同时隐藏在按钮上的鼠标点击事件上,但我希望能够通过单击菜单movieclip及其按钮外部来隐藏所述菜单。是否有可能在as3中实现这一目标?
到目前为止,这是我找到,改编并尝试使用的解决方案,但仍然无法实现。
switchbd_btn.addEventListener(MouseEvent.CLICK, onClickHandler);
stage.addEventListener(MouseEvent.CLICK, onClickHandler);
function onClickHandler(event : MouseEvent) : void {
switch(event.target)
{
case switchbd_btn:
switchbd.gotoAndStop(2);
switchbdIN.start();
switchbd_btn.filters = [ONf];
break;
case stage:
switchbdOUT.start();
switchbd.gotoAndStop(3);
switchbd_btn.filters = [OFf];
break;
}
}
我原来的工作代码: -
//////////////////////SWITCH BOARD\\\\\\\\\\\\\\\\\\\\\\\\\\\\
switchbd_btn.addEventListener(MouseEvent.MOUSE_DOWN, ShowswitchBD);
var switchbdIN:Tween = new Tween (switchbd, "x", Strong.easeOut, 1089.05, 277.85, 1, true);
var switchbdOUT:Tween = new Tween (switchbd, "x", Strong.easeOut, 277.85, 1089.05, 1, true);
var ONf:DropShadowFilter = new DropShadowFilter (-1,26,0xCCCCCC,1,2,2,1,3,false,false,false);
var OFf:DropShadowFilter = new DropShadowFilter (6,26,0x000000,1,2,2,1,3,false,false,false);
function ShowswitchBD(e:MouseEvent):void {
if (switchbd.currentFrame != 2)
{
switchbd.gotoAndStop(2);
switchbdIN.start();
switchbd_btn.filters = [ONf];
}
else {
switchbdOUT.start();
switchbd.gotoAndStop(3);
switchbd_btn.filters = [OFf];
}
}
switchbd是菜单movieclip,switchbd_btn是按钮。
答案 0 :(得分:0)
这是一种完成你所要求的模式。这假定您的菜单movieClip被调用:switchbd
并使用按钮显示/隐藏它:switchbd_btn
。
首先,在按钮上添加一个单击侦听器并创建处理函数:
switchbd_btn.addEventListener(MouseEvent.CLICK, toggleMenu);
function toggleMenu(e:Event):void {
//the x position should be 277.85 when visible
if(switchbd.x <= 278){
hideMenu();
}else{
showMenu();
}
}
现在创建节目和隐藏菜单功能:
function showMenu():void {
switchbd.gotoAndStop(2); //or however you 'show' it
switchbdIN.start();
switchbd_btn.filters = [ONf];
//since the menu is visible, listen for global mouse clicks to hide it
stage.addEventListener(MouseEvent.CLICK, hideMenuGlobalClick);
}
function hideMenu():void {
switchbd.gotoAndStop(3);
switchbdOUT.start();
switchbd_btn.filters = [OFf];
//stop listening for global mouse clicks since the menu is hidden now
stage.removeEventListener(MouseEvent.CLICK, hideMenuGlobalClick);
}
function hideMenuGlobalClick(e:Event):void {
var target:DisplayObject = e.target as DisplayObject;
//crawl up the display tree from the item click until there is no parent (stage)
while(target && target.parent){
//if the target is the menu or button, exit this function
if(target == switchbd || target == switcbd_btn){
return;
}
//move on to the next item up the chain
target = target.parent;
}
//if you made it this far, it wasn't the menu that was clicked
hideMenu();
}