I'm trying to implement parallax background in my game..
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();
}
}
My libgdx game is running perfectly for somtimes on real devices. But after opening my game again after some time it shows black screen and get closed without any message. I have carefully disposed Spritebatch, stage, and all textures.
Any idea what can I do more? I have read many questions regarding it here and on Google and did whatever considerable solutions are available but no luck. I have also checked this Black Screen After Restart but no luck for me.
I had a similar error. For me the problem was a memory leak error.
On your code the memory leak could happen beacause you inizialized the SpriteBatch on the render method.
@Override
public void render(float delta) {
update(delta);
SpriteBatch sb = new SpriteBatch(); //<- here
...
Try to inizialize it on the show() method.