Search code examples
javaandroidlibgdx

LibGDX reverse background image


I did some basic parallax scrolling effect here -

float speed = (float)ps / divideSpeed;

    if(flip == "right") {
        scrollSpeed += speed;
        addImage = 1;
    } else if(flip == "left") {
        scrollSpeed -= speed;
        addImage = -1;
    }

     dayTop.setU(scrollSpeed);
     dayTop.setU2(scrollSpeed + addImage);

The variable speed contains the speed it wishes to move the background, and then it checks for the player's flip, if the player is looking right we want the value to be positive, and if the player is looking left the value will be negative, simple enough.

When the player is moving right everything is all right, but when he is moving left the background just flip his x coords, and i want it to just reverse the background.

Any ideas how to implement it?

I hope you understand, Thanks in advance! :)

EDIT: Was a very easy fix, thanks for helping, no one helped actually, so i thank myself i suppose ;)


Solution

  • So I am going to answer my own question because somebody asked me.
    Here's what I wrote -

    scrollSpeed += deltaTime;
    
    background.setU(scrollSpeed);
    background.setU2(scrollSpeed + 1.0f);
    

    Okay, let me explain it, I add deltaTime to the scrollSpeed every frame.
    I set the scrollSpeed to the background, and lastly I set the scrollSpeed + 1.0f to the background.

    What the 1.0f means? it means how much of the image we want to see in our screen.
    when we set it to 1.0f it will fit the whole background image to the screen.
    when we set it to 2.0f it will fit 2 whole background images to the screen(duplicated).
    when we set it to 0.5f it will fit only half of the background image to the screen ( it will stretch the image ).

    Just play with it a bit and I am sure you will understand.