【问题标题】:Inconsistent (flickering) mouse coordinates in InputListener.touchDraggedInputListener.touchDragged 中的鼠标坐标不一致(闪烁)
【发布时间】:2016-10-11 21:26:27
【问题描述】:

我有一个演员,我想通过触摸拖动来移动它。

class Tile extends Actor {
    Tile (char c) {
        addListener(new InputListener() {
            private float prevX, prevY;

            @Override
            public void touchDragged (InputEvent event, float x, float y, int pointer) {
                Gdx.app.log(TAG, "touchDrag: (" + x + "," + y);
                Tile cur = (Tile)event.getTarget();
                cur.setPosition(  //this call seems to cause the problem
                        cur.getX() + (x - prevX),
                        cur.getY() + (y - prevY) );
                prevX = x; prevY = y;
            }
        });
    }

    @Override
    public void draw(Batch batch, float alpha) {
        batch.draw(texture, getX(), getY());
    }

}

瓷砖在被拖动时会颤抖,移动速度约为触摸速度的一半。这由输出坐标的日志行确认,如下所示:

I/Tile: touchDrag: (101.99991,421.99994)
I/Tile: touchDrag: (112.99985,429.99994)
I/Tile: touchDrag: (101.99991,426.99994)
I/Tile: touchDrag: (112.99985,433.99994)
I/Tile: touchDrag: (101.99991,429.99994)
I/Tile: touchDrag: (112.99985,436.99994)

如果我删除注释行(即不重置演员的位置),拖动输出看起来更合理:

I/Tile: touchDrag: (72.99997,78.99994)
I/Tile: touchDrag: (65.99997,70.99994)
I/Tile: touchDrag: (61.99997,64.99994)
I/Tile: touchDrag: (55.99997,58.99994)
I/Tile: touchDrag: (51.99997,52.99994)
I/Tile: touchDrag: (42.99997,45.99994)

有什么想法吗?感谢收看!

【问题讨论】:

    标签: android libgdx scene2d


    【解决方案1】:

    InputListener 方法中的坐标是相对于 Actor 的位置给出的,因此如果您同时移动 Actor,它们无法与之前的值进行比较。

    相反,存储原始位置并相对于该位置移动。数学计算出来以适应您的动作:

        addListener(new InputListener() {
            private float startX, startY;
    
            @Override
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                startX = x;
                startY = y;
                return true;
            }
    
            @Override
            public void touchDragged (InputEvent event, float x, float y, int pointer) {
                Tile cur = (Tile)event.getTarget();
                cur.setPosition(
                        cur.getX() + (x - startX),
                        cur.getY() + (y - startY) );
            }
        });
    

    【讨论】:

    • 非常感谢!像魅力一样工作。我在InputListener 文档中没有看到这个事实。你知道这个事实是否记录在任何地方?
    • 在我看来,文档中缺少它。请记住,scene2d 坐标始终在本地演员的坐标系中给出。
    猜你喜欢
    • 2011-01-23
    • 2014-10-21
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2016-06-09
    • 2018-12-06
    • 1970-01-01
    相关资源
    最近更新 更多