我正在尝试通过右键单击并使用AS3和flash打开ContextMenu来解决我需要获取点击位置的问题。
我尝试过使用MouseEvent.Context_Menu:
mapSprite.addEventListener(MouseEvent.CONTEXT_MENU, OnContextMenuOpening);
这会在右键单击时触发,但上下文菜单不再打开。
我也尝试过使用MosueEvent.Right_Click:
mapSprite.addEventListener(MouseEvent.RIGHT_CLICK, OnContextMenuOpening);
点击右键+点击但会阻止上下文菜单打开。
任何人都对此问题有任何见解?
答案 0 :(得分:2)
不是我认为理想的东西,但这会让你真正理解。创建一个新的菜单对象(具有所有内置菜单选项),并向其添加一个事件监听器。
var my_menu:ContextMenu = new ContextMenu();
contextMenu = my_menu;
my_menu.addEventListener(ContextMenuEvent.MENU_SELECT, rightClicker)
function rightClicker(e:ContextMenuEvent):void {
trace(e);
}
答案 1 :(得分:1)
Atriace的答案基本上是正确的,但它并没有直接解决实际问题,我立即意识到这一点。以下是他的解决方案的一点扩展:
private function setupSample():void
{
// NOTE: Sample assumes that I have a TextField named _myTextField defined elsewhere.
_myTextField.contextMenu = new ContextMenu();
_myTextField.contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, hContextMenuSelect);
}
private function hContextMenuSelect(pEvent:ContextMenuEvent):void
{
var x:Number = pEvent.mouseTarget.mouseX;
var y:Number = pEvent.mouseTarget.mouseY;
}
使用此方法,实际上将显示上下文菜单,但您也可以访问鼠标单击的坐标。只需将ContextMenuEvent.MENU_SELECT侦听器直接添加到上下文菜单中,然后从侦听器参数中提取该点。请注意,鼠标位置可通过ContextMenuEvent获得。
答案 2 :(得分:0)
以下代码正常运行。我创建了一个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;
}
}
}