以下是我尝试添加touchDragged的代码,我尝试使用drag方法通过使用touchDragged功能使对象左右移动。

问题是,当我在屏幕右侧滑动时,无论是向左滑动还是向右滑动,对象仅向右移动。当我在左侧滑动时是向左还是向右,则对象仅向左移动。

有什么我想念的吗?

输入类,然后是对象类。

倾斜动作也与此相同还是有所不同?
如果可以解释的话,那就太好了。

输入类别为

public class Input implements InputProcessor
{
    private Kame myKame;

    public Input(Kame kame) {

        myKame = kame;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        myKame.onClick();
        return true; // Return true to say we handled the touch.
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        myKame.onClick();
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        myKame.onSwipe(screenX, screenY, pointer);
        return true;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}


和Kame类是:

    public class Kame
    {
  private Vector2 position;
  private Vector2 velocity;
  private Vector2 acceleration;

private int width;
private int height;
public float x;

public Kame(float x, float y, int width, int height) {
    this.width = width;
    this.height = height;
    position = new Vector2(x, y);
    velocity = new Vector2(0, 0);
    acceleration = new Vector2(0, 460);
}

public void update(float delta) {

}

public boolean onSwipe(int velocityX, int velocityY, int delta) {

    position.add(position.cpy().scl(delta));

    // if (Math.abs(velocityX) < Math.abs(velocityY)) {
        if (velocityX > 136) {
            position.x += 3;//x cordinate
            velocity.y += 10;
        } else if (velocityX < 136) {
            position.x -= 3;
            velocity.y += 10;
        } else {
            // Do nothing.
        }
    // } else {
        // Ignore the input, because we don't care about up/down swipes.
    // }
    return true;
}

public float getX() {
    return position.x;
}

public float getY() {
    return position.y;
}

public float getWidth() {
    return width;
}

public float getHeight() {
    return height;
}
}

最佳答案

从更改代码

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}

@Override
public boolean touchDragged(int screenX, int screenY, int button) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}


试试看

09-04 22:22