libgdx安卓游戏无法正常渲染

时间:2016-01-23 06:13:58

标签: android libgdx

我的游戏在桌面上工作正常,但当我在Android设备或模拟器上加载时,我得到一个黑屏。游戏正在运作;我可以听到声音并单击UI按钮。在我的Android手机上测试时,我可以关闭屏幕并再次打开,然后游戏呈现正常。我很感激我对错误的解释。

public class TestProject extends Game {

   GameScreen gameScreen;
   EndScreen endScreen;
   MenuScreen menuScreen;

   @Override
   public void create () {
      Gdx.app.log("TestProject", "created");
      AssetLoader.load();
      gameScreen = new GameScreen(this);
      endScreen = new EndScreen(this);
      menuScreen = new MenuScreen(this);
      setScreen(menuScreen);
   }

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

public class MenuScreen implements Screen{

    private SpriteBatch spriteBatch;
    final static float GAME_WIDTH = 200;
    final static float GAME_HEIGHT = 100;
    public static final float UNIT_SCALE = 1/8f;
    static float aspectRatio;
    private OrthographicCamera cam;
    Viewport viewport;
    Stage stage;
    TestProject project;

    public MenuScreen(TestProject theProject){
        project = new TestProject();
        this.project = theProject;

        aspectRatio = (float)Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth();

        spriteBatch = new SpriteBatch();

        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        cam = new OrthographicCamera();
        viewport = new ExtendViewport(GAME_WIDTH * aspectRatio, GAME_HEIGHT, cam);
        viewport.apply();
        cam.setToOrtho(false, GAME_WIDTH, GAME_HEIGHT);
        cam.position.set(GAME_WIDTH / 2, GAME_HEIGHT / 2, 0);

        stage.addActor(AssetLoader.playButton);
    }

    @Override
    public void render(float delta){
        //Clear screen
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        spriteBatch.setProjectionMatrix(cam.combined);

        AssetLoader.font.getData().setScale(.25f, -.25f);
        AssetLoader.playButton.setPosition((Gdx.graphics.getWidth() / 2) - AssetLoader.playButton.getWidth() / 2,
                (Gdx.graphics.getHeight() / 3) - AssetLoader.playButton.getHeight() / 2);

        spriteBatch.begin();

        spriteBatch.draw(AssetLoader.menuBG, 0, 0, 200, 100);

        AssetLoader.font.draw(spriteBatch, "Shark Run", 55, 85);

        spriteBatch.end();

        stage.draw();

    }

    @Override
    public void show() {
        AssetLoader.playButton.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                project.setScreen(project.gameScreen);
            }
        });
    }

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

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }
}

0 个答案:

没有答案