Context Menu

So, i have viewed a lot of flash files over the last few years and noticed that a large amount of them don't bother with the context menu. A few just hide it completely, well, as much as flash allows you to anyway. This isn't bad but you could get a lot more out of it if you wanted to. You can add your own items to that menu and even get them to perform functions within the flash movie. When i first came across this i found it quite hard to understand but none the less i soldiered on with it until i got it. Since then i have used it in all of my projects for many different things but the main use is to link back to your websites if your flash movie isn't hosted there or if you host it on a few different sites.

So to get started, open up an actionscript 3 file. Select the first frame and open up the actions panel(press F9).

!!!Remember that all code is case sensitive and should be copied exactly else it may not work!!!

Put this code in the panel:

--------------------------------------------------------------------------

var myMenu:ContextMenu = new ContextMenu();

myMenu.hideBuiltInItems();

var item1:ContextMenuItem = new ContextMenuItem("what you want to see goes in here");

myMenu.customItems.push(item1);

item1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, openLink);

function openLink(event:ContextMenuEvent):void
{
navigateToURL(new URLRequest("http://www.dansgeeklife.co.nr"));
};

contextMenu = myMenu;

--------------------------------------------------------------------------

Explaining the code:

var myMenu:ContextMenu = new ContextMenu();
This is creating a new variable, naming it myMenu, telling flash what type of variable it is ContextMenu, and telling flash its a new ContextMenu.

myMenu.hideBuiltInItems();
This does what it says... It hides the standard built in items. You can hide them individually if you want to, just look on the adobe as3 help files to find all the options.

var item1:ContextMenuItem = new ContextMenuItem("what you want to see goes in here");
This creates a new context menu item, change the last bit to what you want to see like your website name, more games etc.

myMenu.customItems.push(item1);
This adds the newly created menu item into your custom menu. Without doing this the item would not show up.

item1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, openLink);
This adds an event listener to the menu item so when you click it it performs your function.

function openLink(event:ContextMenuEvent):void
This creates the function to be run on clicking your new menu item. The event type is ContextMenuEvent telling flash to listen for that.

navigateToURL(new URLRequest("http://www.dansgeeklife.co.nr"));
This tells flash to look up an internet address, a new URL request is made directed to the website defined by you.

contextMenu = myMenu;
This tells flash that its context menu is to be replaced with your new context menu.

Now test the file and right click. The menu should now show the item you defined and when clicked should take you to the website you defined.

There are a few other options to look at too, like, you can add an item to the menu and stop it from being selectable. This is good for adding copyright information, your name and the date it was made for example. To do this just put the line:
item1.enabled = false;
After the line defining the item1 variable. Of coarse doing this would mean that the item cannot be clicked and therefore cannot run any function so doing this would be useless if you want to use it as a button for doing something within your flash movie. But you don't need to have the items run any function. you can just add them to the menu without them being used for anything.

Also, to add more items to your menu you don't need to make more or the myMenu variables. Just copy out your item1 variable and change its name to item2, then copy out the customItems.push line and change that to item2 too so it would look like:

--------------------------------------------------------------------------

var myMenu:ContextMenu = new ContextMenu();

myMenu.hideBuiltInItems();

var item1:ContextMenuItem = new ContextMenuItem("what you want to see goes in here");
var item2:ContextMenuItem = new ContextMenuItem("what you want to see goes in here");
item2.enabled = false;

myMenu.customItems.push(item1);
myMenu.customItems.push(item2);

item1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, openLink);

function openLink(event:ContextMenuEvent):void
{
navigateToURL(new URLRequest("http://www.dansgeeklife.co.nr"));
};

contextMenu = myMenu;

--------------------------------------------------------------------------

That just about covers the basics of custom context menus, hope it helps!

0 comments:

Post a Comment