Richard Parnaby-King

Web Developer – PHP, Zend Framework and Actionscript 3

Posted on | | 1 Comment

A context menu is a menu that appears when the user does a right mouse click (usually) on the compiled swf. The default context menu that flash player shows comes with a limited set of choices such as ‘Zoom’, ‘Quality’ and playback options.

This post will demonstrate how to customise the context menu using ActionScript3.


In this simple example we will have an empty context menu with a non-interactive copyright notice.

Preview

The Code

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

var my_copyright = new ContextMenuItem("Copyright - 2012");
my_copyright.enabled = false;

my_menu.customItems.push(my_copyright);

contextMenu = my_menu;

If you are implementing this code outside of Flash, for example as an AS3 package, then you will need to import flash.ui.ContextMenu and flash.ui.ContextMenuItem.

Line 1 is creating a new context menu, and line 2 is removing the default items in this new context menu.

Line 4 is creating our copyright notice, and line 5 is disabling it. This will grey the text of the menu item and prevent any ContextMenuEvents being fired if a user clicks on it.

Line 7 adds our copyright to our context menu, with line 9 replacing the default context menu with our new custom context menu.

Extra Credit

Adding a link to your website to the context menu.

I am not going to break down each line of code (as above), but simply comment the code:

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

  public class ContextMenuEventExample extends MovieClip {


    /**
     * Crate a custom context menu, add a copyright notice and link to website.
     * @return void
     */
    public function ContextMenuEventExample():void {
      //custom context menu
      var my_menu:ContextMenu = new ContextMenu();
      my_menu.hideBuiltInItems();
      //copyright notice
      var my_copyright = new ContextMenuItem("Copyright - 2012");
      my_copyright.enabled = false;
      my_menu.customItems.push(my_copyright);
      
      //add link to website
      var my_link = new ContextMenuItem("Created by Richard Parnaby-King");
      my_menu.customItems.push(my_link);

      //add event listener so when menu item is clicked on browser goes to website
      my_link.addEventListener(ContextMenuEvent.MENU_SELECT, gotoWebsite, false, 0, true);

      //set custom context menu
      contextMenu = my_menu;
    }


    /**
     * Force browser to open new window or tab and browse user to website
     * @param ContectMenuEvent event
     * @return void
     */
    private function gotoWebsite(event:ContextMenuEvent):void {
      navigateToURL(new URLRequest('http://richard.parnaby-king.co.uk'), '_blank');
    }
  }
}

Posted By:

Comments

  • Name

    No idea how to implement this. Can’t you post a zipped example for non-flash users just so that they can see how each part of this post is utilised. Sure would be useful.

  • ABOUT

    Having five years of programming experience in PHP, Zend Framework and ActionScript3, I have a very strong working knowledge of object orientated programming.

    I am a PC Gamer! Playing FPS, RTS, RPG and the occasional MMO since 1996 I have a huge number of a variety of fast-paced games.

  • Recent Posts

  • Categories

  • RSS SUBSCRIBE TO OUR FEED

  •