0
\$\begingroup\$

I would like the user to pan around a game world by dragging their finger on a touch screen - just like Google Maps. I tried a few things but I'm not having much success thus far.

Here is where I'm up to.

public class Game extends ApplicationAdapter {
    Stage stage;

    @Override
    public void create () {

        stage = new Stage(new ScreenViewport());
        Gdx.input.setInputProcessor(stage);

        // Create world
        Planet planet = new Planet();
        stage.addActor(planet);

        stage.addListener(new ActorGestureListener() {

            public void pan(InputEvent event, float x, float y, float deltaX, float deltaY) {
                ((OrthographicCamera)stage.getCamera()).translate(deltaX, deltaY);
                stage.getCamera().update();
            }
        });

    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I needed to use negatives of the deltas when doing the translate!

The required code changes are just the one line:

((OrthographicCamera)stage.getCamera()).translate(-deltaX, -deltaY);

It's a bit jumpy when panning though.

I also discovered I can omit stage.getCamera().update(); and it still works

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .