基本上,您可以在评论中找到答案.
>使用InputProcessor
>保存触摸时的触摸位置
>在拖动时检查保存的触摸位置与当前触摸位置之间的距离
一些代码作为示例:
class MyInputProcessor extends InputAdapter
{
private Vector2 touchPos = new Vector2();
private Vector2 dragPos = new Vector2();
private float radius = 200f;
@Override
public boolean touchDown(
int screenX,
int screenY,
int pointer,
int button)
{
touchPos.set(screenX, Gdx.graphics.getHeight() - screenY);
return true;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer)
{
dragPos.set(screenX, Gdx.graphics.getHeight() - screenY);
float distance = touchPos.dst(dragPos);
if (distance <= radius)
{
// gives you a 'natural' angle
float angle =
MathUtils.atan2(
touchPos.x - dragPos.x, dragPos.y - touchPos.y)
* MathUtils.radiansToDegrees + 90;
if (angle < 0)
angle += 360;
// move according to distance and angle
} else
{
// keep moving at constant speed
}
return true;
}
}
最后,您始终可以检查libgdx类的源代码,并查看其工作方式.