In SWF content, any object that inherits from InteractiveObject can be given a context menu by assigning a menu object to its contextMenu property. The menu object assigned to contextMenu can either be of type NativeMenu or of type ContextMenu.
The legacy context menu API classes allow you to use existing ActionScript code that already contains context menus. If you use the ContextMenu class, you must use the ContextMenuItem class with it; you cannot add NativeMenuItem objects to a ContextMenu object, nor can you add ContextMenuItem objects to a NativeMenu object. The primary drawback to using the context menu API is that it does not support submenus.
Although the ContextMenu class includes methods, such as addItem(), that are inherited from the NativeMenu class, these methods add items to the incorrect items array. In a context menu, all items must be added to the customItems array, not the items array. Either use NativeMenu objects for context menus, or use only the non-inherited ContextMenu methods and properties for adding and managing items in the menu.
The following example creates a sprite and adds a simple edit context menu:
var sprite:Sprite = new Sprite();
sprite.contextMenu = createContextMenu()
private function createContextMenu():ContextMenu{
var editContextMenu:ContextMenu = new ContextMenu();
var cutItem:ContextMenuItem = new ContextMenuItem("Cut")
cutItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doCutCommand);
editContextMenu.customItems.push(cutItem);
var copyItem:ContextMenuItem = new ContextMenuItem("Copy")
copyItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doCopyCommand);
editContextMenu.customItems.push(copyItem);
var pasteItem:ContextMenuItem = new ContextMenuItem("Paste")
pasteItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doPasteCommand);
editContextMenu.customItems.push(pasteItem);
return editContextMenu
}
private function doCutCommand(event:ContextMenuEvent):void{trace("cut");}
private function doCopyCommand(event:ContextMenuEvent):void{trace("copy");}
private function doPasteCommand(event:ContextMenuEvent):void{trace("paste");}