Be a Supporter!

As3: All About Context Menus

  • 965 Views
  • 1 Reply
New Topic Respond to this Topic
Xaotik
Xaotik
  • Member since: Mar. 26, 2008
  • Offline.
Forum Stats
Member
Level 15
Programmer
As3: All About Context Menus 2010-07-22 17:30:02 Reply

AS3: Main

Hello Newgrounders,

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
import flash.ui.ContextMenu;

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
UIContextMenu.hideBuiltInItems();
var Thingy1:ContextMenuItem = new ContextMenuItem("Thingy1", false, true, true);
var Thingy2:ContextMenuItem = new ContextMenuItem("Thingy2", true, true, true);
UIContextMenu.customItems.push(Thingy1, Thingy2);

// 2.2) Assigning Listeners
Thingy1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, traceThingy);
Thingy2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, traceThingy);

// 2.3) Declaring The Handler
function traceThingy(event:ContextMenuEvent):void
{
	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);
	trace(traces[place]);
}

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:

Save
Zoom In
Zoom Out
100%
Show All
Quality
Play
Loop
Rewind
Forward
Back
Movie not loaded
About
Print
Show Redraw Regions
Debugger
Undo
Cut
Copy
Paste
Delete
Select All
Open
Open in new window
Copy link

- Captions can not contain any of the following anywhere, which means these terms can not be used at all:

Adobe
Macromedia
Flash Player
Settings

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.

-Sage

xKiRiLLx
xKiRiLLx
  • Member since: Feb. 8, 2007
  • Offline.
Forum Stats
Member
Level 09
Game Developer
Response to As3: All About Context Menus 2010-07-23 02:03:43 Reply

Glad to see people are still writing tuts for the AS3 forum!