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

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers