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="http://www.adobe.com/2006/mxml"
				layout="absolute" creationComplete="init()">
	<mx:Script>
		<!&#91;CDATA&#91;
			import mx.events.FlexEvent;

			protected function init():void
			{
				// TODO Auto-generated method stub
				this.red.graphics.beginFill(0xff0000);
				this.red.graphics.drawRect(75, 75, 50, 50);
				this.red.graphics.endFill();
				
				
				this.green.graphics.beginFill(0x00ff00);
				this.green.graphics.drawRect(37.5, 37.5, 125, 125);
				this.green.graphics.endFill();
				
				
				this.blue.graphics.beginFill(0x0000ff);
				this.blue.graphics.drawRect(0, 0, 200, 200);
				this.blue.graphics.endFill();
				
 				var blueContextMenu:ContextMenu = new ContextMenu();
				var item:ContextMenuItem = new ContextMenuItem("blue");
				blueContextMenu.customItems.push(item);
				blue.contextMenu = blueContextMenu;
								
				var redContextMenu:ContextMenu = new ContextMenu();
				redContextMenu.hideBuiltInItems();
				var ccccc:ContextMenuItem = new ContextMenuItem("red");
				redContextMenu.customItems.push(ccccc);
				red.contextMenu = redContextMenu; 
			}
		&#93;&#93;>
	</mx:Script>
	
	
	<mx:Canvas id="blue">
		<mx:Canvas id="green">
			<mx:Canvas id="red">
			</mx:Canvas>
		</mx:Canvas>
	</mx:Canvas>
</mx:Application>

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.

Previous PostNext Post

Leave a Reply

Your email address will not be published. Required fields are marked *