1
votes

I have a camera in my GameStage class and am trying to translate the camera when the left or right arrow keys are pressed. When I press either key and print the camera x position it changes, but nothing moves (actors on screen stay in the same position). What am I doing wrong?

Screen Render Method:

public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glClearColor(255, 255, 255, 255);

    gameStage.update();
    gameStage.draw();
    gameStage.act(delta);
}

Stage Code Involving the Camera:

public GameStage() {
    super(new ScalingViewport(Scaling.stretch, Lib.WIDTH, Lib.HEIGHT, new OrthographicCamera(Lib.WIDTH, Lib.HEIGHT)));

    initCamera();

    Gdx.input.setInputProcessor(this);
}

public void initCamera() {
    camera = new OrthographicCamera(Lib.WIDTH, Lib.HEIGHT);
    camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0f);
    camera.update();
}

public void updateCamera() {
    if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
        camera.translate(-5, 0);
    } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
        camera.translate(5, 0);
    }

    camera.update();
}

public void update() {
    updateCamera();
}

Thanks :)

1
You have 2 cameras?eldo

1 Answers

2
votes

From you visible code I would say that the problem is you have two cameras. One is created when you are calling super constructor. And one created manually. Even if you are translating the camera you created, for rendering batch will use stages camera.

Remove the camera you created and work only with stages camera.