Implementing Scrolling Background in LibGDX game
- by Vishal Kumar
I am making a game in LibGDX. After working for whole a day..I could not come out with a solution on Scrolling background.
My Screen width n height is 960 x 540. I have a png image of 1024 x 540. I want to scroll the background in such a way that it contuosly goes back with camera
x-- as per camera
I tried many alternatives... drawing the image twice ..did a lot of calculations and many others....
but finally end up with this dirty code
if(bg_x2 >= - Assets.bg.getRegionWidth()) {
//calculated it to position bg .. camera was initially at 15
bg_x2 = (16 -4*camera.position.x);
bg_x1=bg_x2+Assets.bg.getRegionWidth();
}
else{
bg_x1 = (16 -4*camera.position.x)%224; // this 16 is not proper
//I think there can be other ways
bg_x2=bg_x1+Assets.bg.getRegionWidth();
}
//drawing twice
batch.draw(Assets.bg, bg_x2, bg_y);
batch.draw(Assets.bg, bg_x1, bg_y);
The Simple idea is SCROLLING BACKGROUND WITH SIZE SOMEWHAT MORE THAN SCREEN SIZE SO THAT IT LOOK SMOOTH. Even after a lot of search, i didn't find an online solution. Please help.