Search code examples
javaintellij-idealibgdx

Stack Overflow Error in LibGDX when using "this.create()" and "this.render()"


I was following an online tutorial to make a space shooter using LibGDX and IntelliJ IDEA. Everything was working fine until I added the "this.create()" and "this.render()" functions. When they were added it caused at stack overflow error and I have no idea how to go about fixing it.

    package com.mygdx.spacegame;
    import com.badlogic.gdx.Game;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.Input.Keys;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.SpriteBatch;

    public class MainGameClass extends Game {
        public static final float SPEED = 120;
        public SpriteBatch batch;
        Texture img;
        float x;
        float y;

        @Override
        public void create () {
            this.create();
            batch = new SpriteBatch();
            img = new Texture("badlogic.jpg");
        }

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

            if (Gdx.input.isKeyPressed(Keys.UP)){
                y += SPEED * Gdx.graphics.getDeltaTime();
            }
            if (Gdx.input.isKeyPressed(Keys.DOWN)){
                y -= SPEED * Gdx.graphics.getDeltaTime();
            }
            if (Gdx.input.isKeyPressed(Keys.RIGHT)){
                x += SPEED * Gdx.graphics.getDeltaTime();
            }
            if (Gdx.input.isKeyPressed(Keys.LEFT)){
                x -= SPEED * Gdx.graphics.getDeltaTime();
            }

            batch.begin();
            batch.draw(img, x, y);
            batch.end();
        }

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

When I run the code without the functions, it runs perfectly fine and the image moves around the screen using the arrows keys. But when I add the two functions back in I get this error:

Exception in thread "LWJGL Application"com.badlogic.gdx.utils.GdxRuntimeException: java.lang.StackOverflowError at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:13>3) Caused by: java.lang.StackOverflowError at com.mygdx.spacegame.MainGameClass.create(MainGameClass.java:20) at com.mygdx.spacegame.MainGameClass.create(MainGameClass.java:20) at com.mygdx.spacegame.MainGameClass.create(MainGameClass.java:20) at com.mygdx.spacegame.MainGameClass.create(MainGameClass.java:20) at com.mygdx.spacegame.MainGameClass.create(MainGameClass.java:20) at com.mygdx.spacegame.MainGameClass.create(MainGameClass.java:20) at com.mygdx.spacegame.MainGameClass.create(MainGameClass.java:20) at com.mygdx.spacegame.MainGameClass.create(MainGameClass.java:20) at com.mygdx.spacegame.MainGameClass.create(MainGameClass.java:20)

It's been a while since I've used Java and I'm completely new to LibGDX so I wouldn't even know where to start with this one and I can't find any relevant solutions online. Hopefully someone here might know what the problem is.


Solution

  • create method call itself which will create endless calls to create.

    It can't be done without any stopping flag. Just remove this.create();

    public void create () {
                this.create();