调整大小后,未调用LibGDX渲染

时间:2017-05-05 07:42:54

标签: java android libgdx render

我在LibGDX中制作一个简单的2D游戏。这是我的第一个项目。

在我的手机上一切正常,没什么不好(6.0.1),但在其他手机上(无论什么版本)它都无法正常工作。很明显渲染方法不起作用,但我不知道为什么。我已经尝试添加super.render();,但它已将其设为红色,因此无法正常工作或我犯了一些错误。

这是一个菜单屏幕,首先是什么。它比我玩和死都好,比我再次打电话它崩溃了。

public class MenuScreen implements Screen {

private DualisAutosapp game;
private OrthographicCamera camera;
...
  public MenuScreen(DualisAutosapp game)
    {
    super();
    Gdx.app.log("Menu","Started");


    this.game = game;

    camera = new OrthographicCamera();
    camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    Gdx.app.log("Menu","Camera");

    logoTexture = new Texture(Gdx.files.internal("background.png"));
    logoTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
    logo = new Sprite(logoTexture);
    Gdx.app.log("Menu","Background");

    startTexture = new Texture(Gdx.files.internal("start.png"));
    startTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
    start = new Sprite(startTexture);
    Gdx.app.log("Menu","Start");

    carChooseTexture = new Texture(Gdx.files.internal(prefs.getString("playercar", "player/mazda2.png")));
    carChooseTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
    carChoose = new Sprite(carChooseTexture);
    Gdx.app.log("Menu","Car");

    font = new BitmapFont(Gdx.files.internal("font/joystix_monospace.fnt"), Gdx.files.internal("font/joystix_monospace.png"), false);


    batch = new SpriteBatch();
    Gdx.app.log("Menu","batch");

    calculateSpriteLocation(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    Gdx.app.log("Menu","calculate");
    }

   @Override
   public void show() {

   }

   @Override
   public void resize(int width, int height) {
       camera = new OrthographicCamera();
       camera.setToOrtho(false,Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

       calculateSpriteLocation(width,height);
   }

   @Override
   public void render (float delta) {
       Gdx.app.log("Menu","render");
       Gdx.gl.glClearColor(1, 1, 1, 1);
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    ...
   }
   @Override
    public void resume() {
    Gdx.app.log("Menu","Rizjúm");
    carChooseTexture = new Texture(Gdx.files.internal(prefs.getString("playercar", "player/mazda2.png")));
    carChooseTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
    carChoose = new Sprite(carChooseTexture);
    }

    @Override
    public void hide() {

    }

    private void calculateSpriteLocation(int width, int height)
    {
        logo.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        logo.setPosition(width/2-logo.getWidth()/2, height/2-logo.getHeight()/2);

        start.setSize(Gdx.graphics.getWidth()/3.6f, Gdx.graphics.getHeight()/6.4f);
        start.setPosition(width/2-start.getWidth()/2, height/2-start.getHeight()/2);

        carChoose.setSize(Gdx.graphics.getWidth()/3.6f, Gdx.graphics.getHeight()/6.4f);
        carChoose.setPosition(width/2-carChoose.getWidth()/2, height/10-carChoose.getHeight()/2);
    }
}

一切正常,直到它崩溃的渲染日志。

修改

游戏类

public class DualisAutosapp extends Game {

  @Override
  public void create () {
      showMenuScreen();
  }

  public void switchScreen(Screen newScreen){
      Screen previousScreen = getScreen();
      setScreen( newScreen );

      if (previousScreen != null)
      {
          previousScreen.dispose();
      }
  }

  public void showMenuScreen()
  {
      switchScreen(new MenuScreen(this));
  }

  public void showGameScreen()
  {
      switchScreen(new GameScreen(this));
  }

  public void showCarChangeScreen() {
      switchScreen( new CarChange(this));
  }
}

2 个答案:

答案 0 :(得分:1)

首先,我建议您遵循Libgdx API Game / Screen的{​​{3}}方法。如果你没有覆盖Game的生命周期方法,那么它会调用Screen相应的方法。

您可以随时初始化对象,但这不是一个好方法。最好在show接口的Screen方法中初始化所有屏幕对象。

立即在Game类中创建所有屏幕的对象,并仅在setScreen(screen)方法中使用该对象。

@Override
public void show() {
   ..//create objects
}

使用dispose()方法并销毁您在show()中创建的对象。 方法resize()可用于更新具有设备大小的摄像机视口。 根据您的要求使用pause()resume(),例如音乐暂停,但不能用于对象初始化。

答案 1 :(得分:0)

嗯,我认为你不应该每次都把Game类传递给屏幕构造函数,你可以尝试这样的东西进行屏幕管理。

import com.badlogic.gdx.Screen;

public enum MyScreens {

    GAME_SCREEN {
        public Screen getScreenInstance() {
            return new GameScreen();
        }
    },
    MAIN_MENU {
        public Screen getScreenInstance() {
            return new MainMenu();
        }
    },
    SPLASH_SCREEN {
        public Screen getScreenInstance() {
            return new SplashScreen();
        }
    };
    public abstract Screen getScreenInstance();

}



import com.badlogic.gdx.Screen;
import com.badlogic.gdx.utils.IntMap;

public class ScreenManager {

    private static ScreenManager instance;
    private Game game;
    private IntMap<Screen> screens;

    private ScreenManager() {
        screens = new IntMap<Screen>();
    }

    public static ScreenManager getInstance() {
        if (instance == null) {
            instance = new ScreenManager();
        }
        return instance;

    }

    public void initialize(Game game) {
        this.game = game;
    }

    public void show(MyScreens screen) {

        if (game == null) {
            return;
        }
        if (!screens.containsKey(screen.ordinal())) {
            screens.put(screen.ordinal(), screen.getScreenInstance());
        }
        game.setScreen(screens.get(screen.ordinal()));
    }

    public void dispose(MyScreens screen) {
        if (!screens.containsKey(screen.ordinal())) {
            return;
        }
        screens.remove(screen.ordinal()).dispose();
    }

    public void dispose() {
        for (Screen screen : screens.values()) {
            screen.dispose();
        }
        screens.clear();
        instance = null;
    }

}