1
\$\begingroup\$

I have tried to make game with libgdx

I want to create 3x3 grid in the middle of screen I ran the desktop project, it seems that not much problem at all, but if I resize the windows the 3x3 grid move towards the lower left of the screen (I want to make it always in the center of screen regardless of the screen size change)

What are the problem in my code, I am a new to game developing.

public class GameScreen implements Screen {

    MyGdxGame game;
    SpriteBatch batch;
    Texture img, item;

    public GameScreen(MyGdxGame game) {
        this.game = game;

        batch = new SpriteBatch();
        // a 640x480 image for testing background position
        img = new Texture(Gdx.files.internal("640Wx480H.png"));
        // a 64x64 image for grid item
        item = new Texture(Gdx.files.internal("badlogic.jpg"));
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClearColor(1, 1, 1, 1);
        batch.begin();
        batch.draw(img, 0, 0);
        float itemSize = item.getWidth();
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                batch.draw(item, itemSize * i + (Gdx.graphics.getWidth() - (itemSize * 3)) / 2, itemSize * j + (Gdx.graphics.getHeight() - (itemSize * 3)) / 2);
            }
        }

        batch.end();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void show() {
    }

    @Override
    public void hide() {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        img.dispose();
        item.dispose();
    }

}


public class MyGdxGame extends Game {

    GameScreen mainGameScreen;
    private Viewport viewport;
    private OrthographicCamera camera;

    @Override
    public void create() {
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 640, 480);
        viewport = new FitViewport(640, 480, camera);
        mainGameScreen = new GameScreen(this);
        setScreen(mainGameScreen);
    }

    public void resize(int width, int height) {
        viewport.update(width, height);

    }

    public void render(float delta) {
    }

    public void dispose() {

    }
}
\$\endgroup\$
3
\$\begingroup\$

It seems that you set up a Camera and a Viewport but are not using them.

On resize() of the MyGdxGame class, the camera should be updated also:

public void resize(int width, int height) {
        viewport.update(width, height);
        camera.update();
    }

Then, this should be inside your render(float delta) method before the batch.begin():

batch.setTransformMatrix(game.getCamera().view);
batch.setProjectionMatrix(game.getCamera().projection);

For that to work, create a getter for the camera on the MyGdxGame class.

Maybe there's something else missing (I'm doing it off the top of my head), but there's a start.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ What I did, is just adjust the values of camera, there is no need for Viewport. For example, in the resize method, I wrote camera.viewPortWidth = width and camera.viewPortHeight = height. That worked for me. \$\endgroup\$
    – Ajay
    Feb 3 '17 at 19:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.