I have create a custom contextMenu using AS3 and can apply that to the stage. Any movie clip I place onto the stage does not inherit the contextMenu from the stage, i.e. they display the default contextMenu.
How do I apply my custom contextMenu to every child in my application?
[edit]
This is a simplified version of what I have in my main.as file:
var my_menu:ContextMenu = new ContextMenu();
my_menu.hideBuiltInItems();
var my_copyright = new ContextMenuItem("Copyright - 2012");
my_copyright.enabled = false;
my_copyright.separatorBefore = true;
my_menu.customItems.push(my_copyright);
stage.contextMenu = my_menu;
If I right-click on the stage then I get the copyright. If I add a movieclip (or anything else) to the stage then right-click on that, then I get the default context-menu.
[edit]
I have found the problem, and fixed it. I was adding a background image using stage.addChildAt(mc, 0);. For some reason this removes the context menu. Placing the child at 1 fixes this and allows everything to inherit the contextMenu.
Before: http://richard.parnaby-king.co.uk/examples/stackoverflow/stackoverflow.swf
After: http://richard.parnaby-king.co.uk/examples/stackoverflow/stackoverflow-after.swf
I am changing the purpose of the bounty – can someone explain WHY this happens!?
Ok, so after a bit of testing this is what I have. I can’t say it’s that definitively, as flash doesn’t give the events for right-click, so it’s only a guess, but it seems to hold up.
On a side note, you can’t add a context menu to the stage, it won’t allow it, so the lowest item you can add it to is the document class
When you right-click on a DisplayObject, it’ll look for a ContextMenu on that object. It it doesn’t find one, it’ll continue on up the hierarchy for that object looking for one, stopping when it finds one. Something like this:
In this example, if you right click on
child, there’s no menu, so it looks toparent. Here it findscontext menu2so it shows that. However if you right-click onparent2, there’s no menu, so it looks todocumentand here it findscontext menu1.There seems to be a bit of a hack though when you right-click somewhere else on the stage (i.e. somewhere with no graphics). In this case, as the stage can’t have a
ContextMenu(or at least you can’t set one), it seems to decide to use the context menu of the child at depth 0 (normally the document class).When you added your background image at depth 0, you were bumping up your document class to depth 1. Your hierarchy now looks something like this:
I’m assuming you’re adding your context menu to the document class (in this example
context menu1), so unless your document class has some graphics in it, your event would search up to the stage, find no context menu, then try to look for the context menu of child 0 – in this casebgwhich doesn’t have one.You can test this by drawing something in the
graphicsobject of your document class (or clicking on one of the nested elements). If you right click on the graphics, you’ll see your custom menu, even thoughbgis at depth 0. Alternatively, you can add another menu tobgto see what I mean.