我正试图在我的游戏中实现视差背景..
CODE:
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,舞台和所有纹理。
知道我还能做些什么吗?我在这里和谷歌上已经阅读了很多关于它的问题并且做了任何可用的解决方案,但没有运气。我已经在重启后检查了这个黑屏,但对我来说没有运气。
答案 0 :(得分:0)
我有类似的错误。 对我来说,问题是内存泄漏错误。
在你的代码上,由于你在渲染方法上对SpriteBatch进行了初始化,可能会发生内存泄漏。
@Override
public void render(float delta) {
update(delta);
SpriteBatch sb = new SpriteBatch(); //<- here
...
尝试在show()方法中对其进行初始化。