【问题标题】:LibGDX touchDown touchUp on different Actors不同 Actor 上的 LibGDX touchDown touchUp
【发布时间】:2017-01-22 13:10:52
【问题描述】:

在我的 LibGDX 游戏中,我有一组可点击的 Actor。我希望玩家能够在它们之间滑动并获得滑动停止的 Actor。

我不想要GestureListener,我唯一需要的是在另一个 Actor 上生成的 touchDown 上的 touchUp 事件。例如:想想Android的登录屏幕,您可以在其中滑动图案解锁,但我只需要最后一个点/Actor的事件。

我曾尝试使用 touchDown / touchUp,但事件总是在发生 touchDown 的 Actor 上触发。

一个好的解决方案是为每个被滑动击中的 Actor 获取事件,然后获取全局 touchUp 事件。这是否可能以相对可访问的方式 - 例如。无需编写全新的 InputListener?

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    在听众的touchUp() 中,在所有可点击Actor 的父WidgetGroup 上调用hit,以查看手指在松开时位于哪一个。请注意,由于您要将演员添加到组中,因此需要将组添加到舞台而不是直接将小部件添加到舞台。请记住,Actor 的 X 和 Y 是相对于其父级的。

    final WidgetGroup parent = new WidgetGroup();
    
    InputListener inputListener = new InputListener(){
        private final Vector2 tmp = new Vector2();
    
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            return true;
        }
    
        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            event.getTargetActor().localToParentCoordinates(tmp.set(x, y));
            Actor releaseOverActor = parent.hit(tmp.x, tmp.y, true);
            if (releaseOverActor != null){
                //doSomethingTo(releaseOverActor);
            }
        }
    }
    
    for (Actor clickable : clickableActors){
        parent.add(clickable);
        clickable.addListener(inputListener);
    }
    

    以上内容稍微简化了一点。例如,您可能希望仅在使用指针 0 时才返回真值 touchDown。

    【讨论】:

    • 有效!有没有办法将事件发送给在此过程中被刷过的演员?另外,event.getTargetActor().localToParentCoordinates(tmp.set(x, y)); 应该是final Vector2 pos = event.getTarget().localToParentCoordinates(new Vector2(x, y));(忽略我没有使用全局变量的事实,方法名称不同)。不知道你用的是什么libGDX版本,我是1.9.3的。
    • 我可能会覆盖 touchDragged 然后调用 hit 方法...但我想这不会优化,因为它会垃圾邮件创建 Vector2 对象,投影它们并尝试找到一个 Actor每次触摸都会移动。
    • 我看到了我用 getTargetActor 打的错字,但你为什么要不必要地分配和解除分配向量?以我的方式,即使您在 touchDragged 中使用它也不会产生 GC 垃圾邮件,尽管我不明白这样做的目的,除非您想在将鼠标悬停在演员上时也对其进行着色。
    • 是的,我只是在测试,它会被改变,是的,我想给它们着色。 localToParentCoordinates 是否将其计算应用于给定向量?
    • 方法文档中有说明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    相关资源
    最近更新 更多