0
votes

I'm new to libgdx so please bear with me. I'm trying to create a 2D running game and have been trying to implement a scrolling background. So far I have been able to get the background to scroll but after a while it slows down and becomes choppy. Here is how I implemented it:

in my create method

bg = new Texture(Gdx.files.internal("bg1.png"));
bg.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);

and in my render method i have

try {
    sourceX += 15;
} catch (Exception e){
    sourceX=0;
}
game.batch.draw(bg, 0, 0, sourceX, 0, Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

This seems to work but after a while it becomes choppy. Any suggestions would be appreciated.

1
what do you mean by choppy? fps drop? check your framers with an FPSLogger, maybe you have a memory leakMoira
also, why do you have sourceX += 15 in a try block? that can't throw an exceptionMoira
it starts to stutter or lag . it will no longer be a smooth transiationuser6129465
I get that, but what's your framerate? Does it get lower? (I'm assuming yes, but why don't you check?)Moira
I just checked and my framerate is not going downuser6129465

1 Answers

0
votes

Could be floating point precision going down as your numbers reach higher orders of magnitude. Maintain your precision by keeping it at a low range:

sourceX = (sourceX + 15f) % bg.getWidth();

However, you also need to involve the delta time and a speed if you want constant speed that's the same on all devices:

sourceX = (sourceX + Gdx.graphics.getDeltaTime() * 900f) % bg.getWidth();

Make sure sourceX is a float, not an int.