以下代码正在工作。我创建了一个 300x300 的形状并在其上分配了上下文菜单。鼠标位置将显示在左上角的文本框中。
package {
import flash.ui.ContextMenuItem;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.TextFormat;
import flash.display.Sprite;
import flash.text.TextField;
import flash.ui.ContextMenu;
import flash.display.Shape;
/**
* ...
* @author GR
*/
public class Main extends Sprite {
private var cm_item1:ContextMenuItem = new ContextMenuItem("Menu Item 1");
private var cm_item2:ContextMenuItem = new ContextMenuItem("Menu Item 2");
private var cm_item3:ContextMenuItem = new ContextMenuItem("Menu Item 3");
private var cm_item4:ContextMenuItem = new ContextMenuItem("Menu Item 4");
private var txtFormat:TextFormat = new TextFormat("Arial", 20, 0x000000);
private var c_menu:ContextMenu = new ContextMenu();
private var map_mc:MovieClip = new MovieClip();
private var txt:TextField = new TextField();
private var bg:Shape = new Shape();
public function Main():void {
bg.graphics.beginFill(0x00ffff);
bg.graphics.drawRect(0, 0, 300, 300);
bg.graphics.endFill();
map_mc.addChild(bg);
map_mc.x = map_mc.y = 100;
addChild(map_mc);
txt.background = 0xffffff;
txt.border = true;
txt.defaultTextFormat = txtFormat;
txt.width = 200;
addChild(txt);
c_menu.hideBuiltInItems();
c_menu.customItems.push(cm_item1, cm_item2, cm_item3, cm_item4);
map_mc.contextMenu = c_menu;
map_mc.addEventListener(MouseEvent.CONTEXT_MENU, cmFx);
}
private function cmFx(e:MouseEvent):void {
txt.text = "x: " + mouseX + " y: " + mouseY;
}
}
}