This is my third AS3 Tutorial and I have done my best to make it as understandable as possible, however you will need prior knowledge of Event Handling for this tutorial.
A nice tutorial about Events and Handling them can be found here.
Ok, lets begin.
To start off, we need to import the Context Menu class from its location at flash.ui.ContextMenu, this can be achieved with one simple line. (My comments in the code have a chronological order for this tutorial)
// 1) Import Statements
Ok, that was simple, now the next block of code (step 2) details the small things done to modify the context menu.
// 2) Creating The Menu
var UIContextMenu:ContextMenu = new ContextMenu();
// 2.1) Changing Menu's Items
var Thingy1:ContextMenuItem = new ContextMenuItem("Thingy1", false, true, true);
var Thingy2:ContextMenuItem = new ContextMenuItem("Thingy2", true, true, true);
// 2.2) Assigning Listeners
// 2.3) Declaring The Handler
var traces:Array = new Array("Stop pressing the thingy!", "Annoying noob...", ">:(", "Having fun?", "BOOOOOOOM!", "Hey, look over there, its a... uhh... thingy...");
var place:Number = Math.round(Math.random()*5);
Now then, I'll go through this quickly.
-Step 2: Creates the menu.
-Step 2.1: Changes the items in the menu, creating two new items named Thingy1 and Thingy2.
This is the constructor for a context menu item:
new ContextMenuItem(Caption, Separator Before, Enabled, Visible);
-- Caption is the "title" of the menu item.
-- Separator Before is a Boolean(true/false) variable that declares whether or not the item has a separator above it.
-- Enabled is another Boolean which declares whether or not the item is clickable.
-- Visible is a third Boolean to declare whether or not the item can be seen.
And here is some more information (directly from Adobe) that provides some restrictions on context menu items:
-Context Menus can not contain more than 15 items.
-An item's caption requires at least one visible character (so an item named "" will either be ignored or throw an error)
- White Space (blank characters/Newlines/etc) are ignored.
- Captions can not be longer than 100 characters.
- Captions can not be identical to Flash Player-reserved items, any item on the following list will be ignored:
Movie not loaded
Show Redraw Regions
Open in new window
- Captions can not contain any of the following anywhere, which means these terms can not be used at all:
And if your Flash is in a non-English language, you can not use those terms in either English or the language of your Flash.
-Step 2.2: Adds event listeners to the two items
-Step 2.3: Adds a function(handler) to the listeners, the trace function is used to ensure that the code is executing properly.
And finally, the last step will change our movie/game's menu to our custom menu:
// 3) Setting The Movie's Menu To Our Custom Menu
contextMenu = UIContextMenu;
Now if you test your movie, you should see a custom menu and be able to click on Thingy1 and Thingy2 for a trace message.
Helpful comments/suggestions are welcomed below.