如何从LibGdx核心项目访问活动?

时间:2016-05-08 15:11:27

标签: android android-activity libgdx

我需要从LibGdx核心类转到一个Activity,但我的问题是我正在使用一个GameStateManager,这让我很困惑,我发现这个 posts answer。它给我的错误是我的主要核心项目中的课程" MyGdxGame"是空的。

这是我的GameStateManager类:

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();
}
}

这是我的所有州扩展的GameState类

    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();
}

这是我的MyGdxGame类.AndroidStuff是接口。

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();
}
}

1 个答案:

答案 0 :(得分:0)

以防其他人有同样的问题

我在link's answer中完全喜欢,但我没有更改MenuState构造函数,我只是更改了GameStateManager的构造函数,我给它 public MyGdxGame myGdxGame public class GameStateManager(MyGdxGame mygame){this.myGdxgame = mygame;}并且我通过{{1}访问了方法我可以在每个扩展GameState的类中做。