LibGDX. Задержка при перетаскивании объекта - PullRequest
0 голосов
/ 19 апреля 2020

У меня нарисован шарик на экране, и я перетаскиваю его, ну, код работает отлично, для этого я использую класс InputAdapter:

    public class BallProcessor extends InputAdapter {
    private Vector2 position;
    private int xDistance, yDistance;

    public BallProcessor(int posX, int posY) {
        position = new Vector2(posX, posY);
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        //Calculate the distance between the ball and the pointer position(where we touch)
        xDistance = (int)(screenX - position.x);
        yDistance = (int)(screenY - position.y);
        return true;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        //Drag the ball avoiding respawn
        //it's not necesary to touch the ball to drag
        position.x = screenX - xDistance;
        position.y = screenY - yDistance;
        ballParameters();
        return true;
    }

    //To the batch.draw()
    public Vector2 getPosition() {
        return position;
    }

    public void ballParameters() {
        //Keep the ball into the camera viewport
        if (position.x <= Ball.getBallSize() / 2){
            position.x = Ball.getBallSize() / 2;
        }

        if (position.x >= Gdx.graphics.getWidth() - Ball.getBallSize() / 2) {
            position.x = Gdx.graphics.getWidth() - Ball.getBallSize() / 2;
        }

        if (position.y <= Ball.getBallSize() / 2) {
            position.y = Ball.getBallSize() / 2;
        }

        if (position.y >= Gdx.graphics.getHeight() - Ball.getBallSize() / 2) {
            position.y = Gdx.graphics.getHeight() - Ball.getBallSize() / 2;
        }
    }
}

Теперь это имеет значительную задержку Я не знаю, является ли InputAdapter единственным способом перетаскивания объектов без респауна. Есть ли более эффективный способ сделать это? Если я просто использую Gdx.input.getX / Y () в качестве позиции в batch.draw () , объект респаун где я прикасаюсь и не хочу этого. Или скорость исполнения зависит от моего устройства ???

...