在libGDX周围拖一个圆圈

时间:2016-08-12 05:56:04

标签: java android libgdx

我无法获得一段libGDX代码。

if(Gdx.input.isTouched()) {
        setTouchPos();    //set the x and y values of current touch
        cam.unproject(touchPos);

        if(disc.contains(touchPos.x, touchPos.y)) {
            disc.x = touchPos.x - disc.radius / 2; // disc.x becomes current touch position
            disc.y = touchPos.y - disc.radius / 2; // disc.y becomes current touch position
        }
    }

问题是如果手指移动太快,光盘将停止移动。不知何故,光盘的翻译速度不足以跟上它看起来的运动。这有什么理由吗?

1 个答案:

答案 0 :(得分:1)

我想从你的代码中可以看出,你尝试做的是用指针(手指)拖动光盘,然后通过检查指针是否位于光盘内来实现这一目的。

问题是,如果一个指针(鼠标或手指无关紧要)移动,你不会在它移动的路上获得每个位置但只有一些点(它移动的速度越快)你得到的分数)。指针更多"跳跃"而不是真的感动。例如,如果用户引导画笔,图像编辑器仅在这些点之间绘制线条,因此绘制的线条看起来更多"棱角分明"如果刷子快速移动。

回到你的问题:在你最初检查了用户想要拖动光盘之后,设置一个布尔标志。当此标志为true时,将光盘移动到指针所在的每个位置,即使指针位于光盘外部也是如此。仅在释放指针时将此标志重置为false(onMouseUp或其他)。

所以你的代码看起来更像( Pseudocode

if (disc.contains(touchPos.x, touchPos.y)) {
    dragged = true;
}

...

if (dragged) {
    disc.x = touchPos.x - disc.radius / 2; // disc.x becomes current touch position
    disc.y = touchPos.y - disc.radius / 2; // disc.y becomes current touch position
}

...

public onMouseUp() {
     dragged = false;
}