3

目前我正在使用 scene2d 触摸板在屏幕上移动精灵。我想要做的是使用所有屏幕作为触摸板来移动精灵,但不知道从哪里开始。

  • 如果刚刚触摸屏幕,则精灵不应移动。
  • 精灵应该根据用户手指从初始触摸点移动的距离以不同的速度移动。
  • 一旦用户将手指拖到某个半径之外,精灵将继续以恒定速度移动。

基本上它是一个没有实际使用scene2d触摸板的触摸板

4

1 回答 1

4

基本上你在评论中得到了答案。

  1. 使用输入处理器
  2. 保存触摸位置
  3. 检查保存的触摸位置与触摸拖动时的当前触摸位置之间的距离

一个小代码作为例子:

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 类的源代码,看看它是如何完成的。

于 2015-10-25T11:46:37.787 回答