Good question. This will make a nice function request, the ContextMenuEvent.MENU_CLOSED event :)
I think I have half the answer. Here is 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 the mouseChildren value of false, so the clips below can get clicks. You might want to make this transparent. I used this so that you get an event when you right-click on it. When this happens, I set rightClicking to true, that is, I know that the right click was clicked, I'm just waiting for something else. There are two options:
- The user selects an item from the menu.
- The user presses the menu to disappear.
For option 1, if the user selects any of your custom elements, this is cool, you can handle it if not, at least you know what might happen. For option 2, I set a listener for the MOUSE_DOWN event, so if rightClicking was turned on and you got to the mouse to close your menu.
Hope this helps!
I know this looks like a hacked old school as2, and the code is changed from the sample documentation, but this is a thought :)
source share