Protected: +TUT OF THE MONTH

12 12 2008

This content is password protected. To view it please enter your password below:





Make your custom right click menu on AS3

12 12 2008

Just a note that the “Settings” and “About Adobe Flash Player…” buttons are a requirement of the player. It is not possible to remove them via AS.

I am going to show you how to customize the right-click menu in ActionScript 3 as I have done in my lil flashy works. AS3 already provides you with a class that controls the menu: ContextMenu, and classes that handle the properties and events: ContextMenuItem and ContextMenuEvent. It gives you everything you need and nothing you don’t. It’s pretty straight-forward. So here’s a step-by-step (Note: if you’re working in Flex, you will need to import the classes)

1. Create an instance of ContextMenu in order to use it:
var myMenu:ContextMenu = new ContextMenu();

2. This is pretty self-explanatory. Hide the default items:
myMenu.hideBuiltInItems();

3a. Create your custom item as a ContextMenuItem:
var menuItem1:ContextMenuItem = new ContextMenuItem(“All rights reserved eyetuts”);

3b. If you want to add action to your custom item, add the item and then add a listener to listen for the event:
var menuItem2:ContextMenuItem = new ContextMenuItem(“EYETUTS 2008”);
menuItem2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doSomething);

4. Add your custom item to “myMenu”:
myMenu.customItems.push(menuItem1);

5. Tell Flash where your custom menu appears when you right-click. Generally speaking you would want it to be anywhere and everywhere you right-click. But if you want your custom menu to appear only over a button or movie clip, you would just change “this” to the button or movie clip. 
this.contextMenu = myMenu;

6. Finally, define your listener function for the menu item that contains the action. In this example, it takes you to my site:
function doSomething(e:ContextMenuEvent):void {
var url:String = “https://eyetuts.wordpress.com”;
var request:URLRequest = new URLRequest(url);
navigateToURL(request, ‘_blank’);
}

That’s it. Export  …. eyetuts 2008