Actionscript 3 自定义上下文菜单未显示,因为显示对象位于;“在上面”;

Actionscript 3 自定义上下文菜单未显示,因为显示对象位于;“在上面”;,actionscript-3,flash,Actionscript 3,Flash,作为另一个问题的后续: 我在一个flash应用程序中构建了一个自定义的contextmenu项,但它有时无法显示。 我发现问题在于另一个精灵在自定义上下文菜单项的“顶部” 然而,即使将“mouseEnabled”和“mouseChildren”属性设置为false,我仍然无法让自定义上下文菜单显示。。。 有什么想法吗?谢谢 ps:下面是一些代码来查看问题: var spr:Sprite=new Sprite(); spr.graphics.beginFill(0xff0000,1); spr.g

作为另一个问题的后续: 我在一个flash应用程序中构建了一个自定义的contextmenu项,但它有时无法显示。 我发现问题在于另一个精灵在自定义上下文菜单项的“顶部”

然而,即使将“mouseEnabled”和“mouseChildren”属性设置为false,我仍然无法让自定义上下文菜单显示。。。 有什么想法吗?谢谢

ps:下面是一些代码来查看问题:

var spr:Sprite=new Sprite();
spr.graphics.beginFill(0xff0000,1);
spr.graphics.drawRect(0,0,100,100);
addChild(spr);

var blocker:Sprite=new Sprite();
blocker.x=50
blocker.y=50
blocker.graphics.beginFill(0x00ff00,1);
blocker.graphics.drawRect(0,0,100,100);
addChild(blocker);
blocker.mouseEnabled=false
blocker.mouseChildren=false

var customContextMenu:ContextMenu = new ContextMenu();
var item:ContextMenuItem = new ContextMenuItem("custom item");
customContextMenu.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler,false,0,true);
spr.contextMenu = customContextMenu;

function menuItemSelectHandler(cem:ContextMenuEvent) {
    trace("hello context");
};

当鼠标位于绿色矩形上时,不会显示自定义上下文菜单项

据我所知,仅当用户右键直接单击某个对象本身时,才会显示该对象的上下文菜单

我已在以下代码中简化了您的问题:

public class Test extends Sprite
{
    public function Test()
    {
        stage.align = StageAlign.TOP_LEFT;
        stage.scaleMode = StageScaleMode.NO_SCALE;

        var sprite:Sprite = new Sprite();
        addChild(sprite);

        sprite.graphics.beginFill(0xFF0000);
        sprite.graphics.drawRect(0, 0, 200, 200);
        sprite.graphics.endFill();

        var shape:Shape = new Shape();
        addChild(shape);

        shape.graphics.beginFill(0x0000FF, .6);
        shape.graphics.drawRect(100, 100, 200, 200);
        shape.graphics.endFill();

        setUpContextMenu(sprite);
    }

    private function setUpContextMenu(target:InteractiveObject):void
    {
        var menu:ContextMenu = new ContextMenu();
        target.contextMenu = menu;

        var item:ContextMenuItem = new ContextMenuItem("About Us");
        menu.customItems.push(item);
    }
}
在红色和蓝色方块重叠的区域上单击鼠标右键时,不会出现自定义关联菜单

这里有一个可能的解决方案,我只修改了
setUpContextMenu()
函数:

    private function setUpContextMenu(target:InteractiveObject):void
    {
        var menu:ContextMenu = new ContextMenu();
        this.contextMenu = menu;

        var item:ContextMenuItem = new ContextMenuItem("About Us");

        var handler:Function = function (event:ContextMenuEvent):void {
            // Start with empty menu.
            var items:Array = [];

            if (event.mouseTarget == target) {
                // Directly right-clicked on target. Add custom item.
                items = [item];

            } else if (event.mouseTarget is DisplayObjectContainer) {
                var o:DisplayObjectContainer
                    = DisplayObjectContainer(event.mouseTarget);
                var pt:Point = o.localToGlobal(new Point(o.mouseX, o.mouseY));
                var arr:Array = o.getObjectsUnderPoint(pt);

                // One of the mouse target's descendants is our target,
                // directly under the pointer. Add custom item.
                if (arr.indexOf(target) != -1)
                    items = [item];
            }

            menu.customItems = items;
        };

        menu.addEventListener(ContextMenuEvent.MENU_SELECT, handler);
    }

这里我将上下文菜单分配给应用程序本身。在
“menuSelect”
事件中,我根据鼠标指针是否位于红方块上方的某个位置对其进行了自定义。

1)一些代码总是有用的。2) 在同一层中有精灵和关联菜单吗?3) 您是否尝试将contextMenu的childIndex设置为子列表中的最高值?您好,让我们更清楚一点:自定义contextMenu不是精灵,而是普通上下文菜单中的一项。我在原来的帖子中添加了一些代码