scrolling never-ending background in Game not working as expected

204 views Asked by At

I am working on my first side-scroller game and am trying to achieve a neverending background effect and it's nearly working as i would expect but there are little glitches.

I have 2 instances of the background (each fills the screen width), with one placed at the bottom left corner of the screen and one off the screen to the right. I am then moving my camera each frame to the right and when the first background instance is completely offscreen to the left i reset its X to the right of the camera so that it is now (relative to the camera) off the right of the screen. this is working a lot of the time, but every now and again it seems the method to reset its x position is getting called a few frames late and results in a gap in the background.

the update code i am using in the main game is -

private void update(float delta){
    //update camera and world
        camera.position.set(camera.position.x+scrollSpeed, camera.position.y, 0);
        camera.update();
        world.step(step, velocityIterations, positionIterations);

        if(gameInProgress){ 
            //backgrounds (array holds the 2 instances of the background)
                for(int i=0; i< bgFillArray.size; i++){
                    bgFillArray.get(i).update();
                }
        }
    stage.act(delta);
    tweenManager.update(delta); 
}

the update method in the BgFill class -

public void update(){
    float currentXPos = getX();
    float leftBoundary = camera.position.x-1200;
    float rightOfScreen = camera.position.x+400;

    if(active){
        //check to see if the camera has gone past this instance. if so then move to right
            if(currentXPos <= leftBoundary){
                setX(rightOfScreen);
            }
    }
}

Firstly, is this the usual way (or even close) to do a continuous scrolling background?

if it is, what am I doing wrong?

0

There are 0 answers