Managing Context Menus in AS3 with the Reyco1 AS3 Tools
Reynaldo February 3rd, 2008
Hi all:
A context menu is a menu for a display object that pops up when right-clicking on it; also called a short-cut menu.
Using the Reyco1 Framework makes managing context menus really easy and very fast. First we need to import the necessary files:
1 2 | import com.reyco1.manager.ContextMenuManager; import com.reyco1.events.ContextMenuManagerEvent; |
Now we create two instances of the ContextMenuManager class. One for the stage and one for a box movieclip on the stage.
3 4 | var stageCM:ContextMenuManager = new ContextMenuManager(this); var boxCM:ContextMenuManager = new ContextMenuManager(mcBox); |
Now, using the addMenuItem method, we add a few menu items to each instances of the ContextMenuManager we just created. The addMenuItem method accepts 3 arguments: a string to represent the menu label, a boolean specifying if there should be separator before the menu item or not and another boolean specifying if the menu item is enabled.
5 6 7 8 9 | stageCM.addMenuItem("Hello World!"); stageCM.addMenuItem("Hello Moon!"); stageCM.addMenuItem("Hello Star!", true); boxCM.addMenuItem("Move to 0,0") |
We then have each of the ContexMenuManager instances listen to the ContextMenuManagerEvent.MENU_SELECTED event which is fired each time a context menu item is selected.
10 11 | stageCM.addEventListener(ContextMenuManagerEvent.MENU_SELECTED, handleMenuSelection); boxCM.addEventListener(ContextMenuManagerEvent.MENU_SELECTED, handleBoxMenuSelection); |
Finally, we create the event handler functions. Every ContextMenuManagerEvent event carries along with it a params object. This object contains two values “label” and “target”.
12 13 14 15 16 17 18 19 20 21 22 23 24 | function handleMenuSelection($event:ContextMenuManagerEvent):void { output.text = "the selected menu is: "+$event.params.label } function handleBoxMenuSelection($event:ContextMenuManagerEvent):void { if($event.params.label == "Move to 0,0"){ output.text = "moving "+$event.params.target.name+" to 0,0."; $event.params.target.x = 0; $event.params.target.y = 0; } } |
That’s it! There are other properties and methods that you can use, but basically these are all you need to get started. The sample fla can be found in the samples folder of the Reyco1 Framework.



