Search code examples
androidandroid-activitylibgdx

How can I access an Activity from LibGdx core project?


I need to just go to an Activity from LibGdx core classes, but my problem is that I am using a GameStateManager which is confusing me with this posts answer I have found.The error it gave me was that my main class in core project "MyGdxGame" was null.

This is my GameStateManager class:

public class GameStateManager {

private GameState gameState;

public static final int MENU = 0;
public static final int PLAY = 1;
public static final int HIGHSCORESTATE = 2;
public static final int BALLSTORE = 3;
public static final int CHARACTERSTORE = 4;
public static final int WINAPRIZE = 5;

public GameStateManager(){
    setState(MENU);
}

public void setState(int state){
    if(gameState != null)gameState.dispose();

    if(state == MENU){
        gameState = new MenuState(this);
    }
    if(state == PLAY){
        gameState = new PlayState(this);
    }
    if(state == HIGHSCORESTATE){
        gameState = new HighScoreState(this);
    }
    if(state == BALLSTORE){
        gameState = new BallStoreState(this);
    }
    if(state == CHARACTERSTORE){
        gameState = new CharacterStoreState(this);
    }
    if(state == WINAPRIZE){
        gameState = new WinPrizeState(this);
    }
}

public void update(float dt){
    gameState.update(dt);
}

public void draw(){
    gameState.draw();
}
}

And this is my GameState class that all my states extend

    public abstract class GameState {

protected GameStateManager gsm;

protected GameState(GameStateManager gsm){
    this.gsm = gsm;
    init();

}

public abstract void init();
public abstract void update(float dt);
public abstract void draw();
public abstract void handleInput();
public abstract void dispose();
}

And this is my MyGdxGame class.AndroidStuff is the Interface.

public class MyGdxGame extends ApplicationAdapter {

public static int WIDTH;
public static int HEIGHT;
public static OrthographicCamera camera;
private GameStateManager gsm;

public MyGdxGame(AndroidStuff androidStuff) {
    super();
    this.androidStuff = androidStuff;
}

@Override
public void create () {

    WIDTH = Gdx.graphics.getWidth();
    HEIGHT = Gdx.graphics.getHeight();

    camera = new OrthographicCamera();
    camera.translate(WIDTH/2, HEIGHT/2);
    camera.update();

    gsm = new GameStateManager();

}

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

    gsm.update(Gdx.graphics.getDeltaTime());
    gsm.draw();
}
}

Solution

  • Just in case anyone else has the same problem

    I did exactly like in the link's answer, but I didn't change the MenuState constructor, I just changed the GameStateManager's constructor and I gave it public MyGdxGame myGdxGame public class GameStateManager(MyGdxGame mygame){this.myGdxgame = mygame;} and I accessed the method by gsm.mygdx.androidStuff.startActivity() which I can do in every class that extends GameState.