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 ...
If you found this post useful, please consider a small donation.
» 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.

8 Responses

  1. JK says:

    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 says:

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

  3. Xavi says:

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

  4. Monkios says:

    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 says:

    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 says:

    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 says:

    only works with activex flash player, not the netscape.

  8. musquetan says:

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

Leave a Reply

flash games company