0
\$\begingroup\$

My question is very simple I am new to libgdx development, which is the best approach implementing a background that is to make the background move in y direction

1)My First approach is drawing the background image 10 times towards positive y direction using camera.translate(x,y) function to move camera from bottom to top 2)second approach is to make the camera still while making the background scroll ie drawing the background frame by frame updating the position each time

Can you please suggest which is the best approach?

\$\endgroup\$
1
  • \$\begingroup\$ "The best way" doesn't really exist - if what you're planning works then great - keep using it. Otherwise it's actually too opinion based to say "X approach is the best way". \$\endgroup\$ May 18, 2015 at 8:56

3 Answers 3

0
\$\begingroup\$

In my game i have 3 background textures (maximum amount that can fit on screen). I initialize them so they are placed in a row. And this is how i update them:

    protected void updateBackground(Background background) {
        if (background.position.x < camera.position.x
                - (background.width + camera.viewportWidth / 2))
            background.position.x += 3 * background.width;
    }

This code just checks if background texture is out of screen and if so places it in front of other background textures.

\$\endgroup\$
0
\$\begingroup\$

I would have one background sprite. Then offset the uv of that sprite for when the camera moves. The sprite's texture will have to be set to repeating. The background sprite will need to be locked to camera position.

Very similar to this Repeat texture in a scrolling game

\$\endgroup\$
2
  • \$\begingroup\$ Yeah,I have tried this approach but when I run my android game on mobile ....background turns black..on desktop it works perfect... \$\endgroup\$
    – Shersha Fn
    May 19, 2015 at 15:19
  • \$\begingroup\$ @ShehinFn Does it turn black even if you don't modify the UVs? \$\endgroup\$ May 24, 2015 at 12:54
0
\$\begingroup\$

I have done something like this and it works great! Thank you all for your valuable suggesitions!

initiate()
{
  bg1 = new Sprite( backgroundImage );
  bg2 = new Sprite( backgroundImage );
}

update()
{
  position1 = position1 - 3;
  position2 = position2 - 3;

  if( position2 <= -height )
    position2 = height;

  if( position1 <= -height )
    position1 = height;
}

render()
{
  bg1.setPosition( positionX, position2 );
  bg2.setPosition( positionX, position1 );
  bg2.draw( batch );
  bg1.draw( batch );
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .