Creating a custom context menu item in Flash using ActionScript 3.0

by Peter deHaan on March 5, 2009

in ContextMenu

The following example shows how you can add a custom context menu item to a Sprite object by setting the Sprite’s contextMenu property to a ContextMenu object.

Full code after the jump.

// ActionScript 3.0
var red_cmi:ContextMenuItem = new ContextMenuItem("red");
red_cmi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, cmi_menuItemSelect);
 
var cm:ContextMenu = new ContextMenu();
cm.customItems.push(red_cmi);
cm.hideBuiltInItems();
 
var spr:Sprite = new Sprite();
spr.contextMenu = cm;
spr.graphics.beginFill(0x000000);
spr.graphics.drawRect(0, 0, 120, 90);
spr.graphics.endFill();
spr.x = 10;
spr.y = 10;
addChild(spr);
 
function cmi_menuItemSelect(evt:ContextMenuEvent):void {
    spr.graphics.clear();
    spr.graphics.beginFill(0xFF0000);
    spr.graphics.drawRect(0, 0, 120, 90);
    spr.graphics.endFill();
}

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post: Rotating a Sprite object around its x-axis in Flash using ActionScript 3.0 and Flash Player 10

Next post: Creating a vertical Slider control in Flash using ActionScript 3.0