gpt4 book ai didi

android - InputListener.touchDragged 中的鼠标坐标不一致(闪烁)

转载 作者:行者123 更新时间:2023-11-29 17:13:12 25 4
gpt4 key购买 nike

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

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)

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

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)

有什么想法吗?感谢您的关注!

最佳答案

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

相反,存储原始位置并相对于该位置移动。数学可以适应您的 Action :

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

关于android - InputListener.touchDragged 中的鼠标坐标不一致(闪烁),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39987028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com