Create Right Click Menu in Multiple Parent-Child Layer

On June 11, 2012, in Flash Flex AIR, by James Liu

In Flash, creating right click menu is easy and simple. In one display object which has several cascade children display objects, we can define different right click menu for display objects in different levels. Here I will show a simple example. In this example, I will create a three level display object. The blue color displayObject is the most out parent, which has a child displayobject in green color, and the green color displayObject has one red color child display object.

After creating these displayobject, I am gonna add one customized right click menu on the most outer parent, the blue color display object and add another customized right click menu on the most inner child, the red color display object.

When you right click on the red square, you will get a right click menu which has a menu item called “red”. If you right click on blue area, you will get a right click menu which has a menu item called “blue”. If you right click on the green area, you will get the same menu as what you get on the blue area. It is because we don’t set the customized menu on the green display object and it will bubble the right click event to its parent, the blue DisplayObject. After blue color display object listen the right click event, it will show its right click menu as a response.

Here is the source code. I write it in Flex, and if you want to see the Flash version, please write the comment.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=""
				layout="absolute" creationComplete="init()">

			protected function init():void
				// TODO Auto-generated method stub;, 75, 50, 50);;
				;, 37.5, 125, 125);;
				;, 0, 200, 200);;
 				var blueContextMenu:ContextMenu = new ContextMenu();
				var item:ContextMenuItem = new ContextMenuItem("blue");
				blue.contextMenu = blueContextMenu;
				var redContextMenu:ContextMenu = new ContextMenu();
				var ccccc:ContextMenuItem = new ContextMenuItem("red");
				red.contextMenu = redContextMenu; 
	<mx:Canvas id="blue">
		<mx:Canvas id="green">
			<mx:Canvas id="red">

In the flash player, the ContextMenu object is the right click menu. This attribute is defined in InteractiveObject, the super class of TextField, MovieClip, Sprite, and many other components in Flash. The context menu on TextField is a little bit different from other interactiveObject. TextField context menu includes cut, copy, paste, clear, and select all commands which cannot be removed. More details, you can check Actionscript Reference ContextMenu Section.

Tagged with:  

Leave a Reply

Free WordPress Themes

Free WordPress Themes