自定义文本菜单没有显示,因为显示对象是躺在"在最前面"躺在、自定义、最前面、菜单

2023-09-08 14:11:48 作者:我要住进你心里

作为后续行动,这里另一个问题: 我已经建立在Flash应用程序的自定义文本菜单项,并有一个问题,它不显示的时候。 我发现这个问题是另一个精灵是上面趴对该项目进行自定义文本菜单。

as a follow up to another question here: i've build a custom contextmenu item in a flash application and had a problem with it not showing sometimes. i found out that the issue was that another sprite was lying "on top" of the item with the custom contextmenu.

然而,即使有将mouseEnabled和mouseChildren属性设置为false项目之上我还是不能让自定义文本菜单显示... 有任何想法吗?谢谢!

however, even with the "mouseEnabled" and "mouseChildren" properties set to false for the item "on top" i still cannot get the custom contextmenu to show... any ideas? thanks!

PS:这里是一些code,看问题:

ps: here is some code to see the problem:

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");
};

当鼠标移动到绿色矩形,自定义的ContextMenuItem不显示

when the mouse is over the green rect, the custom contextmenuitem is not shown

推荐答案

为对象的上下文菜单将只有当用户直接用鼠标右键单击该对象本身上显示,据我所知。

The context menu for an object will be displayed only if the user right-clicks directly on that object itself, as far as I know.

我在这个code简化您的问题:

I've simplified your problem in this code:

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);
    }
}

当您在哪里,红色和蓝色方块重叠的区域单击鼠标右键,你不要自定义快捷菜单。

When you right-click on the area where the red and blue squares overlap, you don't get the custom context menu.

下面是一个可能的解决方案,在那里我已经修改只是 setUpContextMenu()功能:

Here's a possible solution, where I've modified only the setUpContextMenu() function:

    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事件,我自定义的基础上的鼠标指针是否是上面的红色方块的地方。

Here I'm assigning the context menu to the application itself. On the "menuSelect" event, I customize it based on whether the mouse pointer is somewhere above the red square.

 
精彩推荐
图片推荐