Custom Context Menu in AS3

April 15th, 2008 by Adrian

I’ve just been having a play with creating a custom context menu in Flash using AS3. Here is the movie …

And here is the ActionScript …

package {
    import flash.display.Sprite;
    import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;
    import flash.events.ContextMenuEvent;
    import flash.net.navigateToURL;
    import flash.net.URLRequest;

    public class Main extends Sprite
    {
        private var menuItemLabel:String = "© Adrian Parr";
        private var url:String = "http://www.adrianparr.com";
        private var cm:ContextMenu;

        public function Main()
        {
            cm = new ContextMenu();
            cm.hideBuiltInItems();
            var cmi:ContextMenuItem = new ContextMenuItem(menuItemLabel);
            cmi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelected);
            cm.customItems.push(cmi);
            this.contextMenu = cm;
        }
       
        private function menuItemSelected(evt:ContextMenuEvent):void
        {
            var req:URLRequest = new URLRequest(url);
            navigateToURL(req, '_blank');
        }
    }
}

You can download the Flash source files here.

Posted in ActionScript 3.0, Flash, Sample Code | 23 Comments »

Hide default right-click menu in AS3

April 1st, 2008 by Adrian

To hide the default right-click menu in AS3 use this …

package {
    import flash.display.Sprite;

    public class Main extends Sprite {
        public function Main():void {
            stage.showDefaultContextMenu = false;
        }
    }
}

or this also works …

package {
    import flash.display.Sprite;
    import flash.ui.ContextMenu;

    public class Main extends Sprite {
        private var cm:ContextMenu;

        public function Main() {
            cm = new ContextMenu();
            cm.hideBuiltInItems();
            this.contextMenu = cm;
        }
    }
}

Posted in ActionScript 3.0, Flash, Sample Code | 6 Comments »