Actionscript 3 ContextMenu关闭事件? [英] Actionscript 3 ContextMenu closed event?

查看:167
本文介绍了Actionscript 3 ContextMenu关闭事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flash的动作脚本中的上下文菜单(右键单击菜单)已打开时,会发送一个事件:

  ContextMenuEvent.MENU_SELECT 

现在,在菜单关闭时是否发送了一个事件? / p>

解决方案

好的问题。
这将使一个不错的功能请求,一个ContextMenuEvent.MENU_CLOSED事件:)



我想我有一半你回答。这是我的想法:

  var myContextMenu:ContextMenu = new ContextMenu(); 
var menuLabel:String =Custom Item;
var rightClicking:Boolean;

addCustomMenuItems();
myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT,menuSelectHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseUpHandler);
var redRectangle = makeRedRectangle();
redRectangle.contextMenu = myContextMenu;


函数makeRedRectangle():Sprite {
redRectangle = new Sprite();
redRectangle.graphics.beginFill(0x990000,.2);
redRectangle.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
redRectangle.mouseChildren = false;
addChild(redRectangle);
返回redRectangle;
}

函数addCustomMenuItems():void {
myContextMenu.hideBuiltInItems();
var item:ContextMenuItem = new ContextMenuItem(menuLabel);
myContextMenu.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menuItemSelectHandler);
}

函数menuSelectHandler(event:ContextMenuEvent):void {
trace(menuSelectHandler:+ event);
rightClicking = true;
}

function menuItemSelectHandler(event:ContextMenuEvent):void {
trace(menuItemSelectHandler:+ event);
}

函数mouseUpHandler(event:MouseEvent):void {
if(rightClicking){
trace('ContextMenu Closed\\\
Thank You!Come Again! );
rightClicking = false;
}
}

基本上我创建一个坐在所有东西之上的精灵,但是mouseChildren设置为false,所以剪辑下面它可以获得点击。你可能想要这个透明。我使用这个,所以当你右键点击它时,你会发现一个事件。当这种情况发生时,我将rightClicking设置为true,意思是,我知道右键被按下,我只是在等待别的事情发生。有两个选项:


  1. 用户从菜单中选择一个项目。

  2. 用户点击对于选项1,如果用户选择任何您的自定义项目,那么很酷,您可以处理,如果不是,至少你知道会发生什么。
    对于选项2,我设置了MOUSE_DOWN事件的侦听器,所以如果右键被打开并且你将鼠标放下,那就是你的菜单关闭。



    希望这有帮助!



    我知道,它看起来像是hacky老学校as2,代码是从文档示例中修改的,但这是一个想法:)


    There is an event which is dispatched when the context menu (right click menu) in actionscript for flash has been opened:

    ContextMenuEvent.MENU_SELECT
    

    Now, is there an event which is dispatched when the menu has been closed?

    解决方案

    Good question. That would make a nice feature request, an ContextMenuEvent.MENU_CLOSED event :)

    I think I have half you're answer. Here's my idea:

    var myContextMenu:ContextMenu = new ContextMenu();
    var menuLabel:String = "Custom Item";
    var rightClicking:Boolean;
    
    addCustomMenuItems();
    myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, menuSelectHandler);
    stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseUpHandler);
        var redRectangle = makeRedRectangle();
        redRectangle.contextMenu = myContextMenu;
    
    
    function makeRedRectangle():Sprite{
        redRectangle = new Sprite();
        redRectangle.graphics.beginFill(0x990000,.2);
        redRectangle.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
        redRectangle.mouseChildren = false;
        addChild(redRectangle);
        return redRectangle;
    }
    
    function addCustomMenuItems():void {
        myContextMenu.hideBuiltInItems();
         var item:ContextMenuItem = new ContextMenuItem(menuLabel);
         myContextMenu.customItems.push(item);
         item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler);
    }
    
    function menuSelectHandler(event:ContextMenuEvent):void {
         trace("menuSelectHandler: " + event);
         rightClicking = true;
    }
    
    function menuItemSelectHandler(event:ContextMenuEvent):void {
         trace("menuItemSelectHandler: " + event);
    }
    
    function mouseUpHandler(event:MouseEvent):void{
        if(rightClicking){
            trace('ContextMenu Closed\nThank You! Come Again!');
            rightClicking = false;
        }
    }
    

    Basically I create a sprite that sits on top of everything, but has mouseChildren set to false, so clips bellow it can get the clicks. You might want to have this one transparent. I used this so you get an event fired when you right click over it. When that happens I set rightClicking to true, meaning, I know the right click was pressed, I'm just waiting for something else to happen. There are two options:

    1. The user selects an item from the menu.
    2. The user click away to make the menu go away.

    For option 1, if the user selects any of your custom items, that's cool, you can handle that, if not, at least you know what might happen. For option 2 I've setup the listener for the MOUSE_DOWN event, so if the rightClicking was turned on and you got to the mouse down, that's your menu closing.

    Hope this helps!

    I know, it looks like hacky old school as2, and the code is modified from the documentation example, but it's a thought :)

    这篇关于Actionscript 3 ContextMenu关闭事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆