Understanding AS3 context menus

You all should know what is a context menu.

A context menu (also called contextual, shortcut, and popup or pop-up menu) is a menu in a graphical user interface (GUI) that appears upon user interaction, such as a right mouse click. A context menu offers a limited set of choices that are available in the current state, or context, of the operating system or application (source: Wikipedia).

There are thousands of tutorials about custom context menus, anyway I am publishing this script to make you understand how to create different context menus according to what you are clicking on.

Look… when you right click on the stage, you get a contextual menu to place the circle in the middle of the stage, and when you click on the circle you get a contextual menu to place it randomly around the stage.

Here it is the commented source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package {
	import flash.display.Sprite;
	// classes to manage context menus
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.events.ContextMenuEvent;
	public class context extends Sprite {
		// this is the circle you see on the stage
		public var the_sphere:sphere=new sphere();
		// creation of two contextual menus
		public var place_randomly_menu:ContextMenu=new ContextMenu;
		public var reset_menu:ContextMenu=new ContextMenu;
		// creation of two contextual menu items
		public var place_randomly_item:ContextMenuItem=new ContextMenuItem("Place randomly");
		public var reset_item:ContextMenuItem=new ContextMenuItem("Reset");
		public function context() {
			// placing the circle on the stage
			addChild(the_sphere);
			// calling the function to place the sphere in the middle of the stage
			// I am passing a null value because it's the same function I am using
			// in the listener, and functions in listeners require a parameter
			reset_sphere(null);
			// adding to the context menu item a listener to execute "place_sphere" function
			place_randomly_item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,place_sphere);
			// hiding default items in the context menu
			place_randomly_menu.hideBuiltInItems();
			// pusing (in this case placing) the menu item in the context menu
			place_randomly_menu.customItems.push(place_randomly_item);
			// assigning the context menu to the circle
			the_sphere.contextMenu=(place_randomly_menu)
			// repeating the same thing whith the other menu, the stage one
			reset_item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,reset_sphere);
			reset_menu.hideBuiltInItems();
			reset_menu.customItems.push(reset_item);
			this.contextMenu=reset_menu;
		}
		public function place_sphere(event:ContextMenuEvent):void {
			the_sphere.x=Math.random()*400+50;
			the_sphere.y=Math.random()*300+50;
		}
		public function reset_sphere(event:ContextMenuEvent):void {
			the_sphere.x=250;
			the_sphere.y=200;
		}
	}
}

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 8 comments

  1. JK

    on March 12, 2009 at 6:21 am

    Thank you! I’ve been wondering how to take control of the right mouse button, and get rid of all that nasty default stuff.

    Is there a way to remove the settings and about flash menu items?

  2. dVyper

    on March 12, 2009 at 3:39 pm

    No there’s no way to get rid of that stuff with any Flash commands.

  3. Xavi

    on March 12, 2009 at 5:58 pm

    would anyone know how to modify the context menu using AS2???

  4. Monkios

    on March 12, 2009 at 7:08 pm

    Nice !

    Does that mean that you tutorials will now have a nice “Reset” button for the ones who actually read your post before looking at the animation and have to reload the page each time ?

  5. yet2bedefined

    on March 12, 2009 at 8:38 pm

    Umm…I’m using Safari 3.1 and it doesn’t seem to be working – all I get is “Settings” and “About Adobe Flash Player”.

  6. musquetan

    on July 13, 2009 at 9:23 am

    Is it possible to define the custom contextMenu just once and have it available in the whole movie and every addChilded moviclip? Or do I have to attach it to every mc that is dynamically created? [working in CS3 with AS3]

  7. breed

    on July 14, 2009 at 12:17 am

    only works with activex flash player, not the netscape.

  8. musquetan

    on July 15, 2009 at 10:46 am

    Alright, it was my fault. My custom cursor (mouseenabled = false) led to the failure. But I could figue this out. So, problem solved. Thanks