【问题标题】:Dart StageXL activate mouse event on overlapping spritesDart StageXL 在重叠的精灵上激活鼠标事件
【发布时间】:2015-11-23 05:02:48
【问题描述】:

是否可以为两个重叠的精灵调用鼠标事件?我曾尝试使用getObjectsUnderPoint,但它似乎不起作用。

class Line extends Sprite
{
    int x;
    int y;
    int type;
    var tempLine = new Shape();
    bool isClicked = false;

    Line(int xPos, int yPos, int type)
    {
        this.x = xPos;
        this.y = yPos;
        this.type = type;

        if(type == 1)
        {
            graphics.beginPath();
            graphics.moveTo(x, y);
            graphics.lineTo(x+300, y);
            graphics.closePath();
            graphics.strokeColor(Color.LightGray,19);
            addEventListener(MouseEvent.CLICK, react);
            tempLine.graphics.beginPath();
            tempLine.moveTo(x,y);
            tempLine.graphics.lineTo(x+300,y);
            tempLine.graphics.closePath();
        }
        else if(type == 2)
        {
            graphics.beginPath();
            graphics.moveTo(x, y);
            graphics.lineTo(x, y+300);
            graphics.closePath();
            graphics.strokeColor(Color.LightGray,19);
            addEventListener(MouseEvent.CLICK, react);
            tempLine.graphics.beginPath();
            tempLine.moveTo(x,y);
            tempLine.graphics.lineTo(x,y+300);
            tempLine.graphics.closePath();
        }
        addChild(tempLine);
    }

    react(MouseEvent event)
    {
        Point tempPoint = new Point(event.localX, event.localY);
        graphics.strokeColor(Color.Black,19);
        isClicked = true;
        var subShape = getObjectsUnderPoint(tempPoint);
        for(Shape i in subShape)
        {
            i.parent.userData.isClicked = true;
        }
    }
}

我有两个重叠的 Line 对象,当单击一个时,我希望两个对象的布尔值都是 true。我读到getObjectsUnderPoint 没有返回Sprite,这可能是问题吗?

【问题讨论】:

    标签: dart mouseevent sprite stagexl


    【解决方案1】:

    MouseEvents 仅调度到扩展 InteractiveObject 类的最顶层显示对象(所有 DisplayObjectContainers 和 Sprites 都是如此)。因此只有一个显示对象可以接收 MouseEvent.CLICK 事件。您是对的,getObjectsUnderPoint 确实只返回 DisplayObjectContainers 的子级,但 GitHub 存储库 (https://github.com/bp74/StageXL/issues/209) 上存在一个未解决的问题,正在讨论这个问题。 StageXL 的下一个版本(高于 0.13 版)可能会改变这种行为。

    【讨论】:

    • 也许我应该更详细地解释一下:您没有使用 getObjectsUnderPoint 方法获得 DisplayObjectContainers (Sprites),因此您不会获得使用 graphics 属性绘制线条的 Sprite。但是您也可以使用具有图形属性的 Shape 类来绘制线条并将此实例作为子对象添加到 Sprite 实例。然后方法 getObjectsUnderPoint 应该返回 Shape 实例。
    • 我更新了代码以解决您的评论,但是现在什么都没有呈现。我可以让Shape 成为Sprite 实例的孩子吗?当我尝试将Shape 的实例添加到Sprite 时,似乎停止了渲染。
    • 是的,Shape 是一个 DisplayObject,每个 DisplayObject 都可以添加到 Sprite 类中,因为 Sprite 是一个 DisplayObjectContainer。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多