Report a paste

Please put a quick comment for the admin.

If it looks like spam, the admin will mark it as spam so that the spam filter picks it up in the future.

If the paste contains something private or offensive, it'll probably just be deleted.

sds

Compare these two implementations. When having a create() method that the class that extends BaseScreen also calls I get a null pointer exception bc of Stage, why?




package com.mygame.ui.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.FitViewport;


public class BaseScreen implements Screen
{
    protected Stage stage;
    protected MyGame myGame;

    public BaseScreen()
    {

    }

    public BaseScreen(MyGame myGame)
    {
        this.myGame = myGame;

        stage = new Stage();
        create();
    }

    public void create()
    {
        // do stuff ...
    }

    @Override
    public void render (float delta)
    {
        if (Gdx.input.isKeyJustPressed(Input.Keys.BACK))
        {
            Gdx.app.exit();
        }

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize (int width, int height)
    {
        stage.getViewport().update(width, height, true);
    }

    @Override
    public void dispose () {
        stage.dispose();
    }

    @Override
    public void show() {
        Gdx.input.setCatchBackKey(true);
    }

    @Override
    public void hide() {
        /* Don't need to do anything here. */
    }

    @Override
    public void pause() {
        /* Don't need to do anything here. */
    }

    @Override
    public void resume() {
       /* Don't need to do anything here. */
    }
}





versus



package com.mygame.ui.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.FitViewport;


public class BaseScreen implements Screen
{
    protected Stage stage;
    protected MyGame myGame;

    public BaseScreen()
    {

    }

    public BaseScreen(MyGame myGame)
    {
        this.myGame = myGame;

        stage = new Stage();
    }

    @Override
    public void render (float delta)
    {
        if (Gdx.input.isKeyJustPressed(Input.Keys.BACK))
        {
            Gdx.app.exit();
        }

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize (int width, int height)
    {
        stage.getViewport().update(width, height, true);
    }

    @Override
    public void dispose () {
        stage.dispose();
    }

    @Override
    public void show() {
        Gdx.input.setCatchBackKey(true);
    }

    @Override
    public void hide() {
        /* Don't need to do anything here. */
    }

    @Override
    public void pause() {
        /* Don't need to do anything here. */
    }

    @Override
    public void resume() {
       /* Don't need to do anything here. */
    }
}