【问题标题】:libgdx Game got black screen and closed(without any message) after some time being playedlibgdx 游戏在玩了一段时间后黑屏并关闭(没有任何消息)
【发布时间】:2018-05-06 08:30:23
【问题描述】:

我正在尝试在我的游戏中实现视差背景..

代码:

public class PlayScreen implements Screen{
private huntRun game;
private OrthographicCamera gamecam;
private Viewport gamePort;
private Hud hud;

private Texture bg;
float sourceX = 0;

private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;

public PlayScreen(huntRun game){
    this.game = game;
    //create cam used to follow character through cam world
    gamecam = new OrthographicCamera();

    bg = new Texture("bg_play.jpg");
    bg.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);

    //create a FitViewport to maintain virtual aspect ration despite screen size
    gamePort = new FitViewport(huntRun.V_WIDTH, huntRun.V_HEIGHT, gamecam);

    //create our game HUD for scores/timers/level info
    hud = new Hud(game.batch);

    maploader = new TmxMapLoader();
    map = maploader.load("ground.tmx");
    renderer = new OrthogonalTiledMapRenderer(map);
    gamecam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
}

@Override
public void show() {

}

public void handleInput(float dt){
    //if right button pressed
    if(hud.flag_R == true){
        gamecam.position.x += 200 * dt;
        sourceX += 100 * dt;
    }
    //if left button pressed
    if(hud.flag_L == true){
        gamecam.position.x -= 200 * dt;
        sourceX -= 100 * dt;
    }
}

public void update(float dt){
    handleInput(dt);
    gamecam.update();
    renderer.setView(gamecam);
}

@Override
public void render(float delta) {
    update(delta);
    SpriteBatch sb = new SpriteBatch();

    float width = 4500;
    float height= 720;
    float uRight = width * 1 / bg.getWidth();
    float vTop= height * 1 / bg.getHeight();

    //Clear the game screen with black
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    sb.setProjectionMatrix(gamecam.combined);
    sb.begin();
    sb.draw(bg, sourceX, 0, width, height, uRight, vTop, 0, 0);
    effect.draw(sb, delta);
    sb.end();
    renderer.render();
    game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
    hud.stage.draw();
}

@Override
public void resize(int width, int height) {
    gamePort.update(width, height);
}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {
    hud.generator.dispose();
    bg.dispose();
}

}

我的 libgdx 游戏有时可以在真实设备上完美运行。但是一段时间后再次打开我的游戏后,它显示黑屏并在没有任何消息的情况下关闭。 Spritebatch、舞台和所有贴图我都仔细处理过。

知道我还能做些什么吗?我在这里和谷歌上阅读了很多关于它的问题,并做了任何可用的相当大的解决方案,但没有运气。重启后我也检查过这个黑屏,但我没有运气。

【问题讨论】:

    标签: java android memory-management libgdx


    【解决方案1】:

    我遇到了类似的错误。 对我来说,问题是内存泄漏错误。

    在您的代码上,可能会发生内存泄漏,因为您在渲染方法上初始化了 SpriteBatch。

    @Override
    public void render(float delta) {
        update(delta);
        SpriteBatch sb = new SpriteBatch();  //<- here
        ...
    

    尝试在 show() 方法中初始化它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      相关资源
      最近更新 更多