Context
Menus
         by eyezberg  :  9 February 2004

From the official Macromedia documentation:

The ContextMenu class provides runtime control over the items in the Flash Player context menu, which appears when a user right-clicks (Windows) or Control-clicks (Macintosh) on Flash Player. You can use the methods and properties of the ContextMenu class to add custom menu items, control the display of the built-in context menu items (for example, Zoom In and Print), or create copies of menus.

You can attach a ContextMenu object to a specific button, movie clip, or text field object, or to an entire movie level. You use the menu property of the Button, MovieClip, or TextField classes to do this.

Having read the official Macromedia definition of this cool new feature of Flash MX2004, let’s see how you can use this.
 

[ right click on the three graphic, text, etc. ]

First, we get rid of (most of) the normal menu items:

var newMenu = new ContextMenu(); //newMenu will be the name of our ContextMenu object
newMenu.hideBuiltInItems(); //this method is the one that hides stuff such as “print”…
/*if you wanted to hide just ceratin items, you’d use newMenu.builtInItems.[propName]=false; where propName can be:
save, zoom, quality,play,loop,rewind,forward, back, and print*/
_root.menu = newMenu; //attached to _root, so valid for the swf in general

If you check out the properties and methods for ContextMenu in the help panel, you’ll see that we got:

  • customItems (an array)

  • hideBuiltInItems (we just used that above..)

And for the menu items themselves, we can use:

  • ContextMenuItem.caption

  • ContextMenuItem.copy

  •  ContextMenuItem.enabled

  • ContextMenuItem.onSelect

  • ContextMenuItem. separatorBefore

  • ContextMenuItem.visible

The names being pretty much self-explanatory!


Now, let’s see how we use these features to build our own menu: (all script and a few clips)

  1. Select frame 1 and open the ActionScript panel.
     

  2. Here, I’m going to run you through just one of the 5 menus you can see in the example fla/swf above. There’s one on _root, one on each clip, one on the button, and you also get new options on selectable textfields and urls as you can see.

// the mail clip:
var mail_cm = new ContextMenu(); //declare new menu object
// create the item, then push into the Item Array of the new menu object:
//name of new item (“Label that gets displayed”, function to execute when clicked) is the idea
var mailItem_cm = new ContextMenuItem("Send Email to Kirupa", mailHandler);
//push the new item into the the menu array
mail_cm.customItems.push(mailItem_cm);
//now define the function you set up above to be called when the item is used (clicked)
function mailHandler(obj, menuItem){
getURL("mailto:kirupa@not_kirupa.com?subject=From the ContextMenu tutorial&body=Hi Kirupa");
}
/*the function can be anything you want, it will have to take these 2 parameters, but then the code to execute is really all up to you, I used (here) a getUrl to open the mail client with a predefined subject and message header; in the swf also a gotoAndPlay/Stop, a regular getURL to open a _blank webpage to the forums, another item linking to Flashkit which is set to disabled, and finally a function to have the bug’s head clip play to another frame, it is really anything you can come up with..!*/
//finally, we need to associate the new menu with a clip (or button, or textfield), and we’re done.
mail_mc.menu = mail_cm;
//do not get confused with the code hint extensions : _cm for contextmenu, _mc for movieclip

That’s it, quite simple, no?

Download Source

Please look at the code in the fla, check the comments, and then get imaginative and  make your own menus; if you come up with something cool, let us know!! ;)

Also, if you have any questions or comments, feel free to post them in the appropriate forum on the kirupaForum.

Cheers,
Eyezberg
flasheyez.com

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.