Saturday, 25 July 2009

Custom context menu in Actionscript 3 - part 2

This is part 2 from the previous Custom context menu in AS3. I will discuss how to add a custom menu to a sprite/movie clip object, and also how to add link functionality to the custom menu.


4. Add Custom context menu to either Sprites/Movie clip objects

You can also add a custom context menu to sprite or movie clip objects. The process it exactly the same, but you need to add a reference to the context menu.

Select any shape tool and create a basic shape on the stage. Convert your shape into a symbol and then give your shape the instance name: “my_symbol”. Open up the actions panel and enter the following code. Most of the code should be familiar only the last line is different.

var my_cMenu:ContextMenu = new ContextMenu();

my_cMenu.hideBuiltInItems();
var my_name = new ContextMenuItem("ilike2flash");
my_cMenu.customItems.push(my_name);

my_symbol.contextMenu = my_cMenu;

Now, try right clicking the shape and areas with no shape. You should notice the custom context menu when you right click on the stage.




5. Add link functionality to the context menu

Adding link functionality to the context menu involves adding an event listener method to the menu item. In the event listener you need to use the ContextMenuEvent Class and the MENU_ITEM_SELECT which will allow you to interact with the objects in the context menu.

Open up the Actions panel and enter the following code:

var my_cMenu:ContextMenu = new ContextMenu();

my_cMenu.hideBuiltInItems();
var my_name = new ContextMenuItem("ilike2flash");
my_cMenu.customItems.push(my_name);

my_name.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, openName);
function openName(event:ContextMenuEvent){
navigateToURL(new URLRequest("http://www.ilike2flash.com"));
}

contextMenu = my_cMenu;

Test your movie Ctrl + Enter. Now, right click and test the link on the context menu.


Related tutorials
Open URL
Mailto button

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP