CUSTOM RIGHT-CLICK MENU IN AS3                                as3


    This very simple tutorial will show you how to create your own custom right-click menu for your flash projects in actionscript 3.0. This project does not require anything else but actionscript. At the you should get something like this ( just right click anywhere on the stage ):


     Open actionscript 3.0 flash document. Since we dont need anything on the stage open the actions window (F9) right away and copy in this actionscript code:

var my_menu:ContextMenu = new ContextMenu();
my_menu.hideBuiltInItems();


var my_link=new ContextMenuItem("URL link");
var my_frame=new ContextMenuItem("go to next frame");
var my_text=new ContextMenuItem("Change text!");
var my_placeholder=new ContextMenuItem("® Trademark");


function openURL(e:ContextMenuEvent):void {
	navigateToURL(new URLRequest("http://www.google.com"));
}
my_link.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, openURL);


function gotoFrame(e:ContextMenuEvent):void {
	gotoAndStop(2);
}
my_frame.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, gotoFrame);


function changeText(e:ContextMenuEvent):void {
	test_txt.text="Text has changed!"
}
my_text.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, changeText);
my_text.separatorBefore=true;
 

function holdPlace(e:ContextMenuEvent):void {
	//your function goes here
}
my_placeholder.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, holdPlace);
my_placeholder.enabled=false;


my_menu.customItems.push(my_link, my_frame, my_text, my_placeholder);
contextMenu=my_menu;

     First line creates new right-click menu. On the second line we have script that hides all the default right-click menu functions like zoom in and out, play,stop etc. If you skip this line, these features will be shown along with your custom menu. On line 5-8 are new menu items. Text in the brackets will be shown in actual menu. Lines 11-14 is definition of one menu item and function assigned to this button. These functions are all your choise. As you can see in this case it will open new window in browser and loads google.com. This repeats for every item of the new menu. Line 27 adds separator line before Change text! item of the menu. Line 34 enables or disables (true, false) one item of the menu. In this case its the ® Trademark one. These two lines are optional, they only help to further customize your right-click menu. Line 37 "pushes" all the new menu items to the new menu variable and line 38 then loads this new menu.


DOWNLOAD SOURCE FILE