1
\$\begingroup\$

I have implemented a basic demo of camera but it is not showing any sprites on screen. I cannot figure out what's wrong with my code.

public class MyGdxGame extends ApplicationAdapter {
    private int width, height;
    private OrthographicCamera camera;

    private SpriteBatch batch;
    private Texture roboTex;

    private Sprite roboSprite;

    private World world;

    private Body robot;

    @Override
    public void create() {
        width = Gdx.graphics.getWidth();
        height = Gdx.graphics.getHeight();

        world = new World(new Vector2(0, 0f), false);
        camera = new OrthographicCamera();
        camera.setToOrtho(false, Constants.viewportWidth,  Constants.viewportWidth *(height/width));
        camera.update();
        batch = new SpriteBatch();

        //textures
        roboTex = new Texture("Images/robot.png");
        roboSprite = new Sprite(roboTex);
        roboSprite.setBounds(0,0,3,6);

    }

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

        batch.begin();
        roboSprite.draw(batch);
        batch.end();

    }

    @Override
    public void resize(int width, int height) {
        float screenAR = width / (float) height;
        camera.setToOrtho(false, Constants.viewportWidth, Constants.viewportWidth/screenAR);
        camera.update();
        batch = new SpriteBatch();
        batch.setProjectionMatrix(camera.combined);
    }


    @Override
    public void dispose() {
        world.dispose();
        roboSprite.getTexture().dispose();
        batch.dispose();
    }

}

Did I missed anything??

\$\endgroup\$
  • \$\begingroup\$ I got it working by deleting batch.setProjectionMatrix(camera.combined); from render method, but why was it causing problem first of all?? \$\endgroup\$ – Shubham Agrawal Apr 6 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.