Implementing Scrolling Background in LibGDX game

Posted by Vishal Kumar on Game Development See other posts from Game Development or by Vishal Kumar
Published on 2013-07-01T08:01:47Z Indexed on 2013/07/01 10:30 UTC
Read the original article Hit count: 1038

Filed under:
|

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.

© Game Development or respective owner

Related posts about libgdx

Related posts about side-scroller