无法切换屏幕[Libgdx]

时间:2015-07-13 16:40:55

标签: java android libgdx

我是libgdx的新手,我无法弄清楚我做错了什么。 在我的游戏中,我只想在2 screen之间切换(第一个是menuScreen,第二个是gameScreen)。我创建了这两个screen和一个游戏类,一切看起来还不错。但是当我调用我的方法时setScreen(new GameScreen())没有任何反应。另外我应该说,我在之前的项目中使用了screengame类,一切都很好,当我将当前代码与之前的代码进行比较时,我没有看到任何差异,所以非常好奇。

这是我的MenuScreen课程:

MenuScreen implements Screen {

private SpriteBatch batch ;
private Texture texture;
private float timePassed;


public MenuScreen() {
    timePassed = 0;
    batch = new SpriteBatch();
    texture = new Texture("texture.png");

@Override
public void render (float delta) {
    timePassed += Gdx.graphics.getDeltaTime();
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    if (timePassed > 5) {GameClass.getInstance().setScreen(new GameScreen());} //it looks strange, but it's to check if all works properly
    batch.begin();
    batch.draw(texture, 0, 0);
    batch.end();
}

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

@Override
public void hide() {

}

@Override
public void resume() {

}

@Override
public void pause() {

}

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

}

@Override
public void show() {

}}

这是我的GameScreen课程:

GameScreen implements Screen {

private SpriteBatch batch2 ;
private Texture texture2;


public MenuScreen() {
    batch2 = new SpriteBatch();
    texture2 = new Texture("texture2.png");
}
@Override
public void render (float delta) {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch2.begin();
    batch2.draw(texture2, 0, 0);
    batch2.end();
}

@Override
public void dispose() {
    batch2.dispose();
    texture2.dispose();
}

@Override
public void hide() {

}

@Override
public void resume() {

}

@Override
public void pause() {

}

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

}

@Override
public void show() {

}}

Game课程:

public class ClassGame extends Game {
public static ClassGame getInstance(){
    ClassGame instance = new ClassGame();
    return instance;
}
@Override
public Screen getScreen() {
    return super.getScreen();
}

@Override
public void setScreen(Screen screen) {
    super.setScreen(screen);
}
@Override
public void resize(int width, int height) {
    super.resize(width, height);
}

@Override
public void render() {
    super.render();
}

@Override
public void resume() {
    super.resume();
}

@Override
public void pause() {
    super.pause();
}

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

public ClassGame() {
    super();
}

@Override
public void create() {
    MenuScreen menuScreen = new MenuScreen();
    setScreen(menuScreen);
}}

2 个答案:

答案 0 :(得分:1)

这就是我为同一个问题所做的工作,我有三个类用于两个屏幕之间切换。在这里,我使用一个图像actor作为按钮在第二个和第三个之间切换。我在每个类中使用了两个构造函数,其中一个构造函数没有参数,下面是我的代码,希望你能得到它。 我的主要类代码:

public class First extends Game{
private Game game;
public Main(){ game=this; }
public void create() {
    game.setScreen(new Second(game)); }}

以下是我的第一个屏幕类代码

public class Second implements Screen{
private Game second;
public Second(Game second){this.second=second;}
public Second(){}
// your code
Stage myStage=new Stage(new ScreenViewport());
Group myGroup=new Group();
Image secondButton=new Image(new Texture(Gdx.files.internal("image.png")));
public void show(){
    myGroup.addActor(secondButton);
    secondButton.addListener(new InputListener(){
    second.setScreen(new Third(second)); });}
public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    myStage.act(Gdx.graphics.getDeltaTime());
     myStage.draw(); }
public void resize(int width, int height) {}
public void pause(){}
public void resume(){}
public void hide(){}
public void dispose(){}}}

我的下一个屏幕类代码

 public class Third implements Screen{
private Game third;
public Third(Game third){
    this.third=third;}
public Third(){}
// your code
Stage myStage=new Stage(new ScreenViewport());
Group myGroup=new Group();
Image thirdButton=new Image(new Texture(Gdx.files.internal("image.png")));
public void show() {
    myGroup.addActor(thirdButton);
    thirdButton.addListener(new InputListener(){
        third.setScreen(new Third(third));});}
public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    myStage.act(Gdx.graphics.getDeltaTime());
    myStage.draw();}
public void resize(int width, int height) {    }
public void pause(){}
public void resume(){}
public void hide(){}
public void dispose(){}}}

答案 1 :(得分:0)

我明白了,伙计们。在我之前的项目中,我在getInstance()之外声明了instance字段。像这样:

    public static GameClass instance = new GameClass();

    public static GameClass getInstance(){
          return instance;
}

但我仍然需要你的帮助。我不知道这件小事怎么不让我换屏幕。