如果鼠标光标在某个坐标范围内,如何显示工具提示?

时间:2022-02-08 23:13:36

Within my component, I'm drawing some rectangles as below:

在我的组件中,我正在绘制一些矩形,如下所示:

                var objGraphics:Graphics=graphics;
                objGraphics.drawRect(start, end, total, 5);
                objGraphics.endFill();

I need to display a custom tooltip for each rectange when the mouse cursor is hovering over it.

当鼠标光标悬停在每个矩形上时,我需要显示一个自定义工具提示。

How can I do this? I'm using the MouseMove event to track when the cursor moves over these coordinates (that part is working), but when I change the tooltip text it's not refreshing.

我怎样才能做到这一点?我正在使用MouseMove事件来跟踪光标在这些坐标上移动的时间(该部分正在工作),但是当我更改工具提示文本时,它不会刷新。

private function this_MOUSE_MOVE(event:MouseEvent):void
        {
//...some code to check the coordinates to find out which rectangle the cursor 
//is over   
        //current tooltip is "Rectangle A";
        ToolTipManager.destroyToolTip(_myToolTip);
        var localPoint:Point=new Point(event.localX, event.localY);
        var globalPoint:Point=new Point(localToGlobal(localPoint).x, 
    localToGlobal(localPoint).y);
        //cursor is over Rectangle B, so changing the tooltip;
        _myToolTip=ToolTipManager.createToolTip("Rectangle B", 
    globalPoint.x, globalPoint.y) as ToolTip;
        callLater(addChild, [_myToolTip]);
}

Thanks for your help.

谢谢你的帮助。

EDIT: The problem seems to be with the following line:

编辑:问题似乎与以下行:

ToolTipManager.destroyToolTip(_myToolTip);

If I comment out the preceding line, it will display the new tooltip, but it will keep creating new ones and the old ones never get removed. But if I add that line, it doesn't add any tooltips! Is the code not being executed sequentially, i.e., is the code to remove the tooltip somehow getting executed after the code to add the tooltip?

如果我注释掉前一行,它将显示新的工具提示,但它将继续创建新的工具提示,旧的工具提示永远不会被删除。但是,如果我添加该行,它不会添加任何工具提示!代码是不是按顺序执行的,即代码是否在代码添加工具提示之后以某种方式删除工具提示?

1 个解决方案

#1


1  

Assuming what you're adding to the stage, is called "myShape", you could do something like this:

假设你正在添加到舞台上,被称为“myShape”,你可以这样做:

// in your class...
private var var tooltip:Tooltip;  // Or whatever your tooltip is

myShape.addEventListener(MouseEvent.MOUSE_OVER, handleOver);
myShape.addEventListener(MouseEvent.MOUSE_OUT, handleOut);

private function handleOver(evt:MouseEvent):void
{
    // Show here
    // OR
    // tooltip = new Tooltip();
    // addChild(tooltip);

}

private function handleOut(evt:MouseEvent):void
{
    // Hide here
    // OR
    // removeChild(tooltip);
}

Hope this helps.

希望这可以帮助。

#1


1  

Assuming what you're adding to the stage, is called "myShape", you could do something like this:

假设你正在添加到舞台上,被称为“myShape”,你可以这样做:

// in your class...
private var var tooltip:Tooltip;  // Or whatever your tooltip is

myShape.addEventListener(MouseEvent.MOUSE_OVER, handleOver);
myShape.addEventListener(MouseEvent.MOUSE_OUT, handleOut);

private function handleOver(evt:MouseEvent):void
{
    // Show here
    // OR
    // tooltip = new Tooltip();
    // addChild(tooltip);

}

private function handleOut(evt:MouseEvent):void
{
    // Hide here
    // OR
    // removeChild(tooltip);
}

Hope this helps.

希望这可以帮助。