Talking about Actionscript 3 and Flash.
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
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;
}
}
}
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.